Why does the Equals implementation for anonymous types compare fields?

Anonymous type instances are immutable data values without behavior or identity. It doesn’t make much sense to reference-compare them. In that context I think it is entirely reasonable to generate structural equality comparisons for them. If you want to switch the comparison behavior to something custom (reference comparison or case-insensitivity) you can use Resharper to … Read more

C# Lambda expressions: Why should I use them?

Lambda expressions are a simpler syntax for anonymous delegates and can be used everywhere an anonymous delegate can be used. However, the opposite is not true; lambda expressions can be converted to expression trees which allows for a lot of the magic like LINQ to SQL. The following is an example of a LINQ to … Read more

How to get index using LINQ? [duplicate]

myCars.Select((v, i) => new {car = v, index = i}).First(myCondition).index; or the slightly shorter myCars.Select((car, index) => new {car, index}).First(myCondition).index; or the slightly shorter shorter myCars.Select((car, index) => (car, index)).First(myCondition).index;

How to String Format [closed]

You can use the string.PadRight() method, coupled with determining which of the array of strings is the widest: var width = line.Max(l => l.Length); foreach (var l in line) Console.WriteLine(l.PadRight(width, ‘.’));