In part 1 of this series I explained the difference between reference equality and value equality. In this article I am going to demonstrate how to compare two reference types using value equality semantics. Override Object.Equals Every time you use the binary equality operator (==) or the Equals method on a reference type you are [...]
Tag Archives: .net
C# 3 gave us the lambda expression and ever since we have been using them to create abstract representations of terse, anonymous functions. While expressive and helpful, they are not always needed. Consider this example: using System; using System.Collections.Generic; class Example { static void Main() { new List<Int32> { 1, 2, 3 } .ForEach(i => [...]
All types are not compared equally
29-Oct-09Here is a bit of a quiz: What does this program print? using System; using System.Collections.Generic; class Person { public String Name { get; set; } } class Example { static void Main() { var people = new List<Person> { new Person { Name = “George Washington” } }; Console.WriteLine( people.Contains( new Person { Name [...]
Know your framework
13-Oct-09What is wrong with the following program? using System; using System.Net; using System.IO; using System.Text; class Example { static void Main() { Console.WriteLine( getWebPage(“http://example.com”)); } static String getWebPage(String url) { WebRequest request = WebRequest.Create(url); WebResponse response = request.GetResponse(); Stream stream = response.GetResponseStream(); StreamReader streamReader = new StreamReader(stream); return streamReader.ReadToEnd(); } } At first glace there [...]