Why does Math.Round(2.5) return 2 instead of 3?

Firstly, this wouldn’t be a C# bug anyway – it would be a .NET bug. C# is the language – it doesn’t decide how Math.Round is implemented. And secondly, no – if you read the docs, you’ll see that the default rounding is “round to even” (banker’s rounding): Return ValueType: System.DoubleThe integer nearest a. If … Read more

If strings are immutable in .NET, then why does Substring take O(n) time?

UPDATE: I liked this question so much, I just blogged it. See Strings, immutability and persistence The short answer is: O(n) is O(1) if n does not grow large. Most people extract tiny substrings from tiny strings, so how the complexity grows asymptotically is completely irrelevant. The long answer is: An immutable data structure built … Read more

Replacing .NET WebBrowser control with a better browser, like Chrome? [closed]

Checkout CefSharp .Net bindings, a project I started a while back that thankfully got picked up by the community and turned into something wonderful. The project wraps the Chromium Embedded Framework and has been used in a number of major projects including Rdio’s Windows client, Facebook Messenger for Windows and Github for Windows. It features … Read more

A generic list of anonymous class

You could do: var list = new[] { o, o1 }.ToList(); There are lots of ways of skinning this cat, but basically they’ll all use type inference somewhere – which means you’ve got to be calling a generic method (possibly as an extension method). Another example might be: public static List<T> CreateList<T>(params T[] elements) { … Read more

ArrayList vs List in C#

Yes, pretty much. List<T> is a generic class. It supports storing values of a specific type without casting to or from object (which would have incurred boxing/unboxing overhead when T is a value type in the ArrayList case). ArrayList simply stores object references. As a generic collection, List<T> implements the generic IEnumerable<T> interface and can … Read more

What are your favorite extension methods for C#? (codeplex.com/extensionoverflow)

public static bool In<T>(this T source, params T[] list) { if(null==source) throw new ArgumentNullException(“source”); return list.Contains(source); } Allows me to replace: if(reallyLongIntegerVariableName == 1 || reallyLongIntegerVariableName == 6 || reallyLongIntegerVariableName == 9 || reallyLongIntegerVariableName == 11) { // do something…. } and if(reallyLongStringVariableName == “string1” || reallyLongStringVariableName == “string2” || reallyLongStringVariableName == “string3”) { // … Read more

How do I truncate a .NET string?

There isn’t a Truncate() method on string, unfortunately. You have to write this kind of logic yourself. What you can do, however, is wrap this in an extension method so you don’t have to duplicate it everywhere: public static class StringExt { public static string Truncate(this string value, int maxLength) { if (string.IsNullOrEmpty(value)) return value; … Read more

Difference between Math.Floor() and Math.Truncate()

Math.Floor rounds down, Math.Ceiling rounds up, and Math.Truncate rounds towards zero. Thus, Math.Truncate is like Math.Floor for positive numbers, and like Math.Ceiling for negative numbers. Here’s the reference. For completeness, Math.Round rounds to the nearest integer. If the number is exactly midway between two integers, then it rounds towards the even one. Reference. See also: … Read more

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