DEV Community

Discussion on: Make a quick URL shortener in C#

Collapse
 
vekzdran profile image
Vedran Mandić

Hey, its a really fun article and I guess very teaching to beginners (hence the fixes Patrick suggested are welcome). What I'd like to point out is that you could've went with LINQ's .Aggregate() fn, e.g. here you can see an exact 6 random chars variation to you code that does not reiterate with foreach but does everything in a single iteration:

  var r = new Random();

  string urlsafe =
     Enumerable.Range(48, 75)
              .Where(i => i < 58 || i > 64 && i < 91 || i > 96)
              .OrderBy(x => r.Next())
              .Aggregate("", (aggr, i) => aggr += Convert.ToChar(i))
              .Substring(0, 6);
Collapse
 
dayvster profile image
Dayvster 🌊

I am aware that I could have used LINQ, I thought about using LINQ. However it would not be very beginner friendly.

Additionally I might have a LINQ specific blog/tut planned ;)

Collapse
 
vekzdran profile image
Vedran Mandić

You already imported and used it with using System.Linq;. What matters is that you do not need an additional iteration with .ToList() just to call .ForEach(), that is what .Aggregate() does for you in a single loop. Good idea for the next article.