Using a class versus struct as a dictionary key

Dictionary<TKey, TValue> uses an IEqualityComparer<TKey> for comparing the keys. If you do not explicitly specify the comparer when you construct the dictionary, it will use EqualityComparer<TKey>.Default. Since neither MyClass nor MyStruct implement IEquatable<T>, the default equality comparer will call Object.Equals and Object.GetHashCode for comparing instances. MyClass is derived from Object, so the implementation will use …

Read more

C# ‘is’ type check on struct – odd .NET 4.0 x86 optimization behavior

I worked up a similar example that fails the same way: using System; using System.Runtime.CompilerServices; public class Program { static void Main() { Console.Write(Verify(Test.Create())); Console.ReadLine(); } //[MethodImpl(MethodImplOptions.NoInlining)] static bool Verify(IDisposable item) { return item is Test; } struct Test : IDisposable { public void Dispose() { } public static Test Create() { return new Test(); …

Read more

Comparing structs to null [duplicate]

It looks like the issue is that when MS introduced nullable types, they made it so that every struct is implicitly convertable to its nullable type (foo?), so the code if( f == null) is equivalent to if ( (Nullable<foo>)f == (Nullable<foo>)null) Since MSDN states that “any user-defined operators that exist for value types may …

Read more

struct sockaddr_un vs. sockaddr

“struct sockaddr” is a generic definition. It’s used by any socket function that requires an address. “struct sockaddr_un” (a “Unix sockets” address) is a specific kind of address family. The more commonly seen “struct sockaddr_in” (an “Internet socket” address) is another specific kind of address family. The cast is what allows the sockets APIs to …

Read more