Interfaces — What’s the point?

No one has really explained in plain terms how interfaces are useful, so I’m going to give it a shot (and steal an idea from Shamim’s answer a bit). Lets take the idea of a pizza ordering service. You can have multiple types of pizzas and a common action for each pizza is preparing the … Read more

What’s the strangest corner case you’ve seen in C# or .NET? [closed]

I think I showed you this one before, but I like the fun here – this took some debugging to track down! (the original code was obviously more complex and subtle…) static void Foo<T>() where T : new() { T t = new T(); Console.WriteLine(t.ToString()); // works fine Console.WriteLine(t.GetHashCode()); // works fine Console.WriteLine(t.Equals(t)); // works … Read more

Logging best practices [closed]

Update: For extensions to System.Diagnostics, providing some of the missing listeners you might want, see Essential.Diagnostics on CodePlex (http://essentialdiagnostics.codeplex.com/) Frameworks Q: What frameworks do you use? A: System.Diagnostics.TraceSource, built in to .NET 2.0. It provides powerful, flexible, high performance logging for applications, however many developers are not aware of its capabilities and do not make … Read more

How do I get a human-readable file size in bytes abbreviation using .NET?

This may not the most efficient or optimized way to do it, but it’s easier to read if you are not familiar with log maths, and should be fast enough for most scenarios. string[] sizes = { “B”, “KB”, “MB”, “GB”, “TB” }; double len = new FileInfo(filename).Length; int order = 0; while (len >= … Read more

Merge two (or more) lists into one, in C# .NET

You can use the LINQ Concat and ToList methods: var allProducts = productCollection1.Concat(productCollection2) .Concat(productCollection3) .ToList(); Note that there are more efficient ways to do this – the above will basically loop through all the entries, creating a dynamically sized buffer. As you can predict the size to start with, you don’t need this dynamic sizing… … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)