tags:

views:

22755

answers:

33

I collect a few corner cases and brain teasers and would always like to hear more. The page only really covers C# language bits and bobs, but I also find core .NET things interesting too. For example, here's one which isn't on the page, but which I find incredible:

string x = new string(new char[0]);
string y = new string(new char[0]);
Console.WriteLine(object.ReferenceEquals(x, y));

I'd expect that to print False - after all, "new" (with a reference type) always creates a new object, doesn't it? The specs for both C# and the CLI indicate that it should. Well, not in this particular case. It prints True, and has done on every version of the framework I've tested it with. (I haven't tried it on Mono, admittedly...)

Just to be clear, this is only an example of the kind of thing I'm looking for - I wasn't particularly looking for discussion/explanation of this oddity. (It's not the same as normal string interning; in particular, string interning doesn't normally happen when a constructor is called.) I was really asking for similar odd behaviour.

Any other gems lurking out there?

+220  A: 

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 fine

        // so it looks like an object and smells like an object...

        // but this throws a NullReferenceException...
        Console.WriteLine(t.GetType());
    }

So what was T...

Answer: any Nullable<T> - such as int?. All the methods are overridden, except GetType() which can't be; so it is cast (boxed) to object (and hence to null) to call object.GetType()... which calls on null ;-p


Update: the plot thickens... Ayende Rahien threw down a similar challenge on his blog, but with a where T : class, new():

private static void Main() {
    CanThisHappen<MyFunnyType>();
}

public static void CanThisHappen<T>() where T : class, new() {
    var instance = new T(); // new() on a ref-type; should be non-null, then
    Debug.Assert(instance != null, "How did we break the CLR?");
}

But it can be defeated! Using the same indirection used by things like remoting; warning - the following is pure evil:

class MyFunnyProxyAttribute : ProxyAttribute {
    public override MarshalByRefObject CreateInstance(Type serverType) {
        return null;
    }
}
[MyFunnyProxy]
class MyFunnyType : ContextBoundObject { }

With this in place, the new() call is redirected to the proxy (MyFunnyProxyAttribute), which returns null. Now go and wash your eyes!

Marc Gravell
Why can't Nullable<T>.GetType() be defined? Shouldn't the result be typeof(Nullable<T>)?
Drew Noakes
Drew: the problem is that GetType() isn't virtual, so it's not overridden - which means that the value is boxed for the method call. The box becomes a null reference, hence the NRE.
Jon Skeet
@Drew; additionally, there are special boxing rules for Nullable<T>, which means that an empty Nullable<T> boxes to null, not a box that contains an empty Nullable<T> (and a null un-boxes to an empty Nullable<T>)
Marc Gravell
Very, very cool. In an uncool kind of way. ;-)
Konrad Rudolph
Wow, thats pretty surprising to me. Had to play around in a test app to see it for myself.
Frank Schwieterman
Could you provide a link to the part of the language documentation that explains what "`where T : new()`" means?
finnw
Constructor-constraint, 10.1.5 in the C# 3.0 langauge spec
Marc Gravell
Wow, thanks for info on `ProxyAttribute`!
Anton Tykhyy
Maybe Java's type erasure wasn't such a bad idea after all.
finnw
@finnw: I really hope that was sarcastic. "Yeah, let's make programmer's lives living hell because nullable types would be boxed to null when calling GetType() on an instance using generics."
configurator
+7  A: 
Public Class Item
   Public ID As Guid
   Public Text As String

   Public Sub New(ByVal id As Guid, ByVal name As String)
      Me.ID = id
      Me.Text = name
   End Sub
End Class

Public Sub Load(sender As Object, e As EventArgs) Handles Me.Load
   Dim box As New ComboBox
   Me.Controls.Add(box)          'Sorry I forgot this line the first time.'
   Dim h As IntPtr = box.Handle  'Im not sure you need this but you might.'
   Try
      box.Items.Add(New Item(Guid.Empty, Nothing))
   Catch ex As Exception
      MsgBox(ex.ToString())
   End Try
End Sub

The output is "Attempted to read protected memory. This is an indication that other memory is corrupt."

Joshua
Interesting! Sounds like a compiler bug, though; I've ported to C# and it works fine. That said, there are a lot of issues with exceptions thrown in Load, and it behaves differently with/without a debugger - you can catch with a debugger, but not without (in some cases).
Marc Gravell
Sorry, I forgot, you need to add the combo box to the form before it will.
Joshua
Is this to do with dialog initialization using an SEH as some kind of horrible internal communication mechanism? I vaguely remember something like that in Win32.
Daniel Earwicker
No. Attaching an unmanaged debugger revealed it to be a null pointer dereference.
Joshua
This is the same problem cbp above. The valuetype being returned is a copy, therefore any references to any properties stemming from said copy are headed to bit-bucket land...http://bytes.com/topic/net/answers/107771-c-struct-class-can-not-access-properties-struct#post382595
Ben
Nope. There are no structs here. I actually debugged it. It adds a NULL to the list item collection of the native combo box causing a delayed crash.
Joshua
+116  A: 

Bankers' Rounding.

This one is not so much a compiler bug or malfunction, but certainly a strange corner case...

The .Net Framework employs a scheme or rounding known as Banker's Rounding.

In Bankers' Rounding the 0.5 numbers are rounded to the nearest even number, so

Math.Round(-0.5) == 0
Math.Round(0.5) == 0
Math.Round(1.5) == 2
Math.Round(2.5) == 2
etc...

This can lead to some unexpected bugs in financial calculations based on the more well known Round-Half-Up rounding.

This is also true of Visual Basic.

Samuel Kim
It seemed strange to me too. That is, at least, until I had round a big list of numbers and calculate their sum. You then realize that if you simply round up, you will end up with potentially huge difference from the sum of the non-rounded numbers. Very bad if you are doing financial calculations!
Tsvetomir Tsonev
Having worked in financial environments I would disagree. If you business requirements need one way of rounding and if your software acts differently, that is where the problems arise. In heavy statistical calculations, Bankers' rounding is more accurate - which is what I think you are referring to.
Samuel Kim
In case people didn't know, you can do: Math.Round(x, MidpointRounding.AwayFromZero);To change the rounding scheme.
ICR
From the docs:The behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding to nearest, or banker's rounding. It minimizes rounding errors that result from consistently rounding a midpoint value in a single direction.
ICR
I wonder if this is why I see `int(fVal + 0.5)` so often even in languages which have a built-in rounding function.
Ben Blank
Nice answer! This drove me insane!!! I couldn't figure out why...
Christian Payne
Ironically, I worked at a *bank* once and the other programmers started flipping out about this, thinking that rounding was broken in the framework
dan
I wrote a stock trade sorting and aggregating app in MS Access years ago (I've done my penance, no need for public humiliation now). If I remember right it did not do banker's rounding and had some nasty side effects due to the fundamental inexactness of real numbers. Ultimately a team and I rewrote the whole thing as a .Net 1.0 ASP.Net app using stored procs. It was a fair bit faster as you might imagine.
Steve Hiner
@Ben Blank: I use that simply because I want an `int`, not a rounded float.
Mark
+1 I hit this one recently
Nij
Yeah! That's one of the reasons I like .NET. Hold on... I don't!!!
victor hugo
This isn't just .net, this goes WAY back to at least VB4. Given this doesn't skew results in most scenarios when aggregating, why isn't this the "normal" way of rounding taught in school?
Jim Leonardo
@Jim Leonardo, in school, .5 gets rounded up. In this code, .5 is rounded up if *up* is even, down if *up* is odd. Given the following array `{ 0.5, 1.5, 2.5, 3.5, 4.5, 5.5 }`, rounding would return `{ 0, 2, 2, 4, 4, 6 }`.
Anthony Pegram
A: 

I think the answer to the question is because .net uses string interning something that might cause equal strings to point to the same object (since a strings are mutable this is not a problem)

(I'm not talking about the overridden equality operator on the string class)

James Z
Strings are *immutable*, not mutable. And this isn't "normal" string interning - it only occurs when you pass in an empty char array. However, the question isn't really "why does this happen?" but "what similar things have you seen?"
Jon Skeet
Reminds me of how any discussion of the Fizz Buzz problem leads to at least half the responses being solutions of the problem.
Wedge
Half of which were incorrect.
Joe
+9  A: 

Interesting - when I first looked at that I assumed it was something the C# compiler was checking for, but even if you emit the IL directly to remove any chance of interference it still happens, which means it really is the newobj op-code that's doing the checking.

var method = new DynamicMethod("Test", null, null);
var il = method.GetILGenerator();

il.Emit(OpCodes.Ldc_I4_0);
il.Emit(OpCodes.Newarr, typeof(char));
il.Emit(OpCodes.Newobj, typeof(string).GetConstructor(new[] { typeof(char[]) }));

il.Emit(OpCodes.Ldc_I4_0);
il.Emit(OpCodes.Newarr, typeof(char));
il.Emit(OpCodes.Newobj, typeof(string).GetConstructor(new[] { typeof(char[]) }));

il.Emit(OpCodes.Call, typeof(object).GetMethod("ReferenceEquals"));
il.Emit(OpCodes.Box, typeof(bool));
il.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new[] { typeof(object) }));

il.Emit(OpCodes.Ret);

method.Invoke(null, null);

It also equates to true if you check against string.Empty which means this op-code must have special behaviour to intern empty strings.

Greg Beech
not to be a smart aleck or anything but have you heard of the [reflector](http://www.red-gate.com/products/reflector/)? it's quite handy in these sorts of cases;
RCIX
You're not being smart; you're missing the point - I wanted to generate specific IL for this one case. And anyway, given that Reflection.Emit is trivial for this type of scenario, it's probably as quick as writing a program in C# then opening reflector, finding the binary, finding the method, etc... And I don't even have to leave the IDE to do it.
Greg Beech
+39  A: 

When is a Boolean neither True nor False?

Bill discovered that you can hack a boolean so that if A is True and B is True, (A and B) is False.

Hacked Booleans

Jonathan Allen
When it's FILE_NOT_FOUND, of course!
Greg
This is interesting because it means, mathematically speaking, that no statement in C# is provable. Ooops.
Simon Johnson
@Simon Johnson - you mean VB.NET, to which that post refers, right?
Daniel Earwicker
I've tested it and it works in c# as well.
RCIX
Someday I shall write a program that depends on this behavior, and the demons of darkest hell will prepare a welcome for me. Bwahahahahaha!
Jeffrey L Whitledge
This example uses bitwise, not logical operators. How is that surprising?
jleedev
Well, he hacks the layout of the struct, of course you'll get weird results, this is not that surprising or unexpected!
Ion Todirel
+101  A: 

What will this function do if called as Rec(0) (not under the debugger)?

static void Rec(int i)
{
    Console.WriteLine(i);
    if (i < int.MaxValue)
    {
        Rec(++i);
    }
}

Answer:

  • On 32-bit JIT it should result in a StackOverflowException
  • On 64-bit JIT it should print all the numbers to int.MaxValue

This is because the 64-bit JIT compiler applies tail call optimisation, whereas the 32-bit JIT does not.

Unfortunately I haven't got a 64-bit machine to hand to verify this, but the method does meet all the conditions for tail-call optimisation. If anybody does have one I'd be interested to see if it's true.

Greg Beech
Has to be compiled in release mode, but most definitely works on x64 =)
Neil Williams
might be worth updating your answer when VS 2010 comes out since all current JITs will then do the TCO in Release mode
ShuggyCoUk
Just tried on VS2010 Beta 1 on 32-bit WinXP. Still get a StackOverflowException.
squillman
Yeah, tail call support in the JIT is only useful if the compiler generates tail opcode prefixes, which it looks like the C# compiler still doesn't do. The equivalent F# code should work perfectly, though. :)
bcat
+1 for the StackOverflowException
stealthdragon
Doesn't work for me on win7 x64 :( throws stack overflow exception
Ilya Biryukov
@Ilya - Are you sure you're compiling the application for the Any CPU or x64 platforms? Visual Studio defaults to x86 so even on a 64-bit system you'll see 32-bit behaviour unless you change the compilation setting.
Greg Beech
@Gred, yeah I am sure :)
Ilya Biryukov
A: 

The following prints False instead of throwing an overflow exception:

Console.WriteLine("{0}", yep(int.MaxValue ));


private bool yep( int val )
{
    return ( 0 < val * 2);
}
Craig Eddy
You can have your OverflowException by wrapping the test in checked{}, or setting the appropriate compiler option. It's not immediately obvious why the default is unchecked... http://msdn.microsoft.com/en-us/library/khy08726(VS.71).aspx
stevemegson
The default is unchecked because the performance hit for doing this check on every integer operation in code is expensive.
Peter Oehlert
Also, the default for VB is to have it all checked. C# compiler team made a different choice for their default trying to more closely what their target audience would expect.
Peter Oehlert
int.MaxValue * 2 is a negative number in unchecked arithmetic, which is the default in C#, there for the comparison returns false. This is not unexpected behavior :P
Lucas
A: 

This one had me truly puzzled (I apologise for the length but it's WinForm). I posted it in the newsgroups a while back.

I've come across an interesting bug. I have workarounds but i'd like to know the root of the problem. I've stripped it down into a short file and hope someone might have an idea about what's going on.

It's a simple program that loads a control onto a form and binds "Foo" against a combobox ("SelectedItem") for it's "Bar" property and a datetimepicker ("Value") for it's "DateTime" property. The DateTimePicker.Visible value is set to false. Once it's loaded up, select the combobox and then attempt to deselect it by selecting the checkbox. This is rendered impossible by the combobox retaining the focus, you cannot even close the form, such is it's grasp on the focus.

I have found three ways of fixing this problem.

a) Remove the binding to Bar (a bit obvious)

b) Remove the binding to DateTime

c) Make the DateTimePicker visible !?!

I'm currently running Win2k. And .NET 2.00, I think 1.1 has the same problem. Code is below.

using System;
using System.Collections;
using System.Windows.Forms;

namespace WindowsApplication6
{
    public class Bar
    {
     public Bar()
     {
     }
    }

    public class Foo
    {
     private Bar m_Bar = new Bar();
     private DateTime m_DateTime = DateTime.Now;

     public Foo()
     {
     }

     public Bar Bar
     {
      get
      {
       return m_Bar;
      }
      set
      {
       m_Bar = value;
      }
     }

     public DateTime DateTime
     {
      get
      {
       return m_DateTime;
      }
      set
      {
       m_DateTime = value;
      }
     }
    }

    public class TestBugControl : UserControl
    {
     public TestBugControl()
     {
      InitializeComponent();
     }

     public void InitializeData(IList types)
     {
      this.cBoxType.DataSource = types;
     }

     public void BindFoo(Foo foo)
     {
      this.cBoxType.DataBindings.Add("SelectedItem", foo, "Bar");
      this.dtStart.DataBindings.Add("Value", foo, "DateTime");
     }

     /// <summary>
     /// Required designer variable.
     /// </summary>
     private System.ComponentModel.IContainer components = null;

     /// <summary>
     /// Clean up any resources being used.
     /// </summary>
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
     protected override void Dispose(bool disposing)
     {
      if (disposing && (components != null))
      {
       components.Dispose();
      }
      base.Dispose(disposing);
     }

     #region Component Designer generated code

     /// <summary>
     /// Required method for Designer support - do not modify
     /// the contents of this method with the code editor.
     /// </summary>
     private void InitializeComponent()
     {
      this.checkBox1 = new System.Windows.Forms.CheckBox();
      this.cBoxType = new System.Windows.Forms.ComboBox();
      this.dtStart = new System.Windows.Forms.DateTimePicker();
      this.SuspendLayout();
      //
      // checkBox1
      //
      this.checkBox1.AutoSize = true;
      this.checkBox1.Location = new System.Drawing.Point(14, 5);
      this.checkBox1.Name = "checkBox1";
      this.checkBox1.Size = new System.Drawing.Size(97, 20);
      this.checkBox1.TabIndex = 0;
      this.checkBox1.Text = "checkBox1";
      this.checkBox1.UseVisualStyleBackColor = true;
      //
      // cBoxType
      //
      this.cBoxType.FormattingEnabled = true;
      this.cBoxType.Location = new System.Drawing.Point(117, 3);
      this.cBoxType.Name = "cBoxType";
      this.cBoxType.Size = new System.Drawing.Size(165, 24);
      this.cBoxType.TabIndex = 1;
      //
      // dtStart
      //
      this.dtStart.Location = new System.Drawing.Point(117, 40);
      this.dtStart.Name = "dtStart";
      this.dtStart.Size = new System.Drawing.Size(165, 23);
      this.dtStart.TabIndex = 2;
      this.dtStart.Visible = false;
      //
      // TestBugControl
      //
      this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.Controls.Add(this.dtStart);
      this.Controls.Add(this.cBoxType);
      this.Controls.Add(this.checkBox1);
      this.Font = new System.Drawing.Font("Verdana", 9.75F,
      System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
      ((byte)(0)));
      this.Margin = new System.Windows.Forms.Padding(4);
      this.Name = "TestBugControl";
      this.Size = new System.Drawing.Size(285, 66);
      this.ResumeLayout(false);
      this.PerformLayout();

     }

     #endregion

     private System.Windows.Forms.CheckBox checkBox1;
     private System.Windows.Forms.ComboBox cBoxType;
     private System.Windows.Forms.DateTimePicker dtStart;
    }

    public class Form1 : Form
    {
     public Form1()
     {
      InitializeComponent();
      this.Load += new EventHandler(Form1_Load);
     }

     void Form1_Load(object sender, EventArgs e)
     {
      InitializeControl();
     }

     public void InitializeControl()
     {
      TestBugControl control = new TestBugControl();
      IList list = new ArrayList();
      for (int i = 0; i < 10; i++)
      {
       list.Add(new Bar());
      }
      control.InitializeData(list);
      control.BindFoo(new Foo());
      this.Controls.Add(control);
     }

     /// <summary>
     /// Required designer variable.
     /// </summary>
     private System.ComponentModel.IContainer components = null;

     /// <summary>
     /// Clean up any resources being used.
     /// </summary>
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
     protected override void Dispose(bool disposing)
     {
      if (disposing && (components != null))
      {
       components.Dispose();
      }
      base.Dispose(disposing);
     }

     #region Windows Form Designer generated code

     /// <summary>
     /// Required method for Designer support - do not modify
     /// the contents of this method with the code editor.
     /// </summary>
     private void InitializeComponent()
     {
      this.components = new System.ComponentModel.Container();
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.Text = "Form1";
     }

     #endregion
    }

    static class Program
    {
     /// <summary>
     /// The main entry point for the application.
     /// </summary>
     [STAThread]
     static void Main()
     {
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      Application.Run(new Form1());
     }
    }
}
Quibblesome
+16  A: 

C# supports conversions between arrays and lists as long as the arrays are not multidimensional and there is an inheritance relation between the types and the types are reference types

object[] oArray = new string[] { "one", "two", "three" };
string[] sArray = (string[])oArray;

// Also works for IList (and IEnumerable, ICollection)
IList<string> sList = (IList<string>)oArray;
IList<object> oList = new string[] { "one", "two", "three" };

Note that this does not work:

object[] oArray2 = new int[] { 1, 2, 3 }; // Error: Cannot implicitly convert type 'int[]' to 'object[]'
int[] iArray = (int[])oArray2;            // Error: Cannot convert type 'object[]' to 'int[]'
Peter van der Heijden
The IList<T> example is just a cast, because string[] already implements ICloneable, IList, ICollection, IEnumerable, IList<string>, ICollection<string>, and IEnumerable<string>.
Lucas
+30  A: 

I'm arriving a bit late to the party, but I've got three four five:

  1. If you poll InvokeRequired on a control that hasn't been loaded/shown, it will say false - and blow up in your face if you try to change it from another thread (the solution is to reference this.Handle in the creator of the control).

  2. Another one which tripped me up is that given an assembly with:

    enum MyEnum
    {
        Red,
        Blue,
    }
    

    if you calculate MyEnum.Red.ToString() in another assembly, and in between times someone has recompiled your enum to:

    enum MyEnum
    {
        Black,
        Red,
        Blue,
    }
    

    at runtime, you will get "Black".

  3. I had a shared assembly with some handy constants in. My predecessor had left a load of ugly-looking get-only properties, I thought I'd get rid of the clutter and just use public const. I was more than a little surprised when VS compiled them to their values, and not references.

  4. If you implement a new method of an interface from another assembly, but you rebuild referencing the old version of that assembly, you get a TypeLoadException (no implementation of 'NewMethod'), even though you have implemented it (see here).

  5. Dictionary<,>: "The order in which the items are returned is undefined". This is horrible, because it can bite you sometimes, but work others, and if you've just blindly assumed that Dictionary is going to play nice ("why shouldn't it? I thought, List does"), you really have to have your nose in it before you finally start to question your assumption.

Benjol
#2 is an interesting example. Enums are compiler mappings to integral values. So even though you didn't explicitly assign them values, the compiler did, resulting in MyEnum.Red = 0 and MyEnum.Blue = 1. When you added Black, you redefined the value 0 to map from Red to Black. I suspect that the problem would have manifested in other usages as well, such as Serialization.
LBushkin
+1 for Invoke required. At ours' we prefer to explicitly assign values to enums like Red=1,Blue=2 so new one can be inserted before or after it will always result in same value. It is specially necessary if you are saving values to databases.
TheVillageIdiot
@aman.tur, you might be interested in this question: http://stackoverflow.com/questions/881726/using-hashcode-to-generate-value-for-enum
Benjol
I disagree that #5 is an "edge case". Dictionary should not have a defined order based on when you insert values. If you want a defined order, use a List, or use a key that can be sorted in a way that's useful to you, or use an entirely different data structure.
Wedge
Well, I guess it's not a corner case, but it's definitely a gotcha (was for me anyway)
Benjol
I spent many hours debugging an InvokeRequired-related bug - very annoying it was.
Tamás Szelei
@Wedge, like SortedDictionary perhaps?
Allon Guralnek
The Dictionary is definitely by design, since it's sorted by hash-code... right?
John Gietzen
#3 happens because constants are inserted as literals everywhere they're used (in C#, at least). Your predecessor might have already noticed it, which is why they used the get-only property. However, a readonly variable (as opposed to a const) would work just as well.
Remoun
#1 is a terrible corner case, which bit us hard. There is an excellent explanation about this here: http://209.85.129.132/search?q=cache:www.ikriv.com/en/prog/info/dotnet/MysteriousHang.html (from Google Cache, because the site seems to be down). It's a very good article, written like a mystery case.
Omer Mor
#2 [Yet another problem with C# enums](http://stackoverflow.com/questions/2875533/what-features-do-you-want-to-see-in-net-5-c-5/2876193#2876193)... #5 This is how hash-tables work. If you need it to be ordered, use [SortedDictionary](http://msdn.microsoft.com/en-us/library/f7fta44c.aspx)
BlueRaja - Danny Pflughoeft
+15  A: 

Here is an example of how you can create a struct that causes the error message "Attempted to read or write protected memory. This is often an indication that other memory is corrupt". The difference between success and failure is very subtle.

The following unit test demonstrates the problem.

See if you can work out what went wrong.

    [Test]
    public void Test()
    {
        var bar = new MyClass
        {
            Foo = 500
        };
        bar.Foo += 500;

        Assert.That(bar.Foo.Value.Amount, Is.EqualTo(1000));
    }

    private class MyClass
    {
        public MyStruct? Foo { get; set; }
    }

    private struct MyStruct
    {
        public decimal Amount { get; private set; }

        public MyStruct(decimal amount) : this()
        {
            Amount = amount;
        }

        public static MyStruct operator +(MyStruct x, MyStruct y)
        {
            return new MyStruct(x.Amount + y.Amount);
        }

        public static MyStruct operator +(MyStruct x, decimal y)
        {
            return new MyStruct(x.Amount + y);
        }

        public static implicit operator MyStruct(int value)
        {
            return new MyStruct(value);
        }

        public static implicit operator MyStruct(decimal value)
        {
            return new MyStruct(value);
        }
    }
cbp
My head hurts... Why doesn't this work?
jasonh
I second this question - can we get an explanation?
rwmnau
Hm i wrote this a few months ago, but I can't remember why exactly it happened.
cbp
Looks like a compiler bug; the `+= 500` calls: `ldc.i4 500` (pushes 500 as an Int32), then `call valuetype Program/MyStruct Program/MyStruct::op_Addition(valuetype Program/MyStruct, valuetype [mscorlib]System.Decimal)` - so it then treats as a `decimal` (96-bits) without any conversion. If you use `+= 500M` it gets it right. It simply looks like the compiler *thinks* it can do it one way (presumably due to the implicit int operator) and then decides to do it another way.
Marc Gravell
The structure is a ValueType so the property is returning a copy. Trying to update a copy of a value type that is going into the bit bucket makes no sense. If you want to update that value, you probably want... MyStruct Temp = bar.Foo; Temp += 500; bar.Foo = Temp;
Ben
Sorry for the double post, here's a more qualified explanation. I'll add this, I have been bit by this and this it sucks, even though I understand why it happens. To me this is an unfortunate limitation of the struct/valuetype. http://bytes.com/topic/net/answers/107771-c-struct-class-can-not-access-properties-struct#post382595
Ben
+46  A: 

Few years ago, when working on loyality program, we had an issue with the amount of points given to customers. The issue was related to casting/converting double to int.

In code below:

double d = 13.6;

int i1 = Convert.ToInt32(d);
int i2 = (int)d;

does i1 == i2 ?

It turns out that i1 != i2. Because of different rounding policies in Convert and cast operator the actual values are:

i1 == 14
i2 == 13

It's always better to call Math.Ceiling() or Math.Floor() (or Math.Round with MidpointRounding that meets our requirements)

int i1 = Convert.ToInt32( Math.Ceiling(d) );
int i2 = (int) Math.Ceiling(d);
jarek
Casting to an integer doesn't round, it just chops it off (effectively always rounding down). So this makes complete sense.
Max Schmeling
@Max: yes, but why does Convert round?
Stefan Steinegger
@Stefan Steinegger If all it did was cast, there would be no reason for it in the first place, would it? Also note that the class name is Convert not Cast.
bug-a-lot
In VB: CInt() rounds. Fix() truncates. Burned me once (http://blog.wassupy.com/2006/01/i-can-believe-it-not-truncating.html)
Michael Haren
+45  A: 

They should have made 0 an integer even when there's an enum function overload.

I knew C# core team rationale for mapping 0 to enum, but still, it is not as orthogonal as it should be. Example from Npgsql.

Test example:

namespace Craft
{
    enum Symbol { Alpha = 1, Beta = 2, Gamma = 3, Delta = 4 };


   class Mate
    {
        static void Main(string[] args)
        {

            JustTest(Symbol.Alpha); // enum
            JustTest(0); // why enum
            JustTest((int)0); // why still enum

            int i = 0;

            JustTest(Convert.ToInt32(0)); // have to use Convert.ToInt32 to convince the compiler to make the call site use the object version

            JustTest(i); // it's ok from down here and below
            JustTest(1);
            JustTest("string");
            JustTest(Guid.NewGuid());
            JustTest(new DataTable());

            Console.ReadLine();
        }

        static void JustTest(Symbol a)
        {
            Console.WriteLine("Enum");
        }

        static void JustTest(object o)
        {
            Console.WriteLine("Object");
        }
    }
}
Michael Buen
Wow that's a new one for me. Also wierd how ConverTo.ToIn32() works but casting to (int)0 doesn't. And any other number > 0 works. (By "works" I mean call the object overload.)
Lucas
There is a recommend code analysis rule to enforce good practices around this behavior: http://msdn.microsoft.com/en-us/library/ms182149%28VS.80%29.aspx That link also contains a nice description of how the 0-mapping works.
Chris Clark
@Chris Clark: i tried putting None = 0 on enum Symbol. still the compiler chooses enum for 0 and even (int)0
Michael Buen
I don't think Chris realized the true point of your post. Has nothing to do with enums as flags.
Jimbo
This is defined in the spec, and I guess the reason is 0 being the default value for all enums. I'm no complier programmer, but I guess the other option is to initialize all enums to the first value in their list of values. Which would be actually a lot more nonsense.
Gorkem Pacaci
+2  A: 

The scoping in c# is truly bizarre at times. Lets me give you one example:

if (true)
{
   OleDbCommand command = SQLServer.CreateCommand();
}

OleDbCommand command = SQLServer.CreateCommand();

This fails to compile, because command is redeclared? There are some interested guesswork as to why it works that way in this thread on stackoverflow and in my blog.

Anders Rune Jensen
I don't view that as particularly bizarre. What you call "perfectly correct code" in your blog is perfectly *incorrect* according to the language specification. It may be correct in some imaginary language you'd *like* C# to be, but the language spec is quite clear that in C# it's invalid.
Jon Skeet
Well it is valid in C/C++. And since it is C# I would have liked it to still work. What bugs me the most is that there is no reason for the compiler to do this. It's not like it hard to do nested scoping. I guess it all comes down to the element of least suprise. Meaning that it can be that the spec says this and that, but that doesn't really help me very much if it's completely illogical that it behaves that way.
Anders Rune Jensen
C# != C/C++. Would you also like to use cout << "Hello World!" << endl; instead of Console.WriteLine("Hello World!");? Also it not illogical, just read the spec.
Lucas McCoy
I am speaking about scoping rules which is part of the core of the language. You are speaking about the standard library. But it's now clear to me that I should simply read the tiny specification of c# language before I start programming in it.
Anders Rune Jensen
Eric Lippert actually posted the reasons why C# is designed like that recently: http://blogs.msdn.com/ericlippert/archive/2009/11/02/simple-names-are-not-so-simple.aspx. The summary is because it's less likely that changes will have unintended consequences.
Helephant
Thanks. That sheds quite a lot of light on it. But really his examples are far fetched to me. Example 1 would simply give a warning in C++. I don't really see they problem. It's also sloppy programming practice to write such big function so I don't really see the point in bending over the language just to fit people who can't write proper code ;-)
Anders Rune Jensen
+33  A: 

Here's one I only found out about recently...

interface IFoo
{
   string Message {get;}
}
...
IFoo obj = new IFoo("abc");
Console.WriteLine(obj.Message);

The above looks crazy at first glance, but is actually legal.No, really (although I've missed out a key part, but it isn't anything hacky like "add a class called IFoo" or "add a using alias to point IFoo at a class").

See if you can figure out why, then: Who says you can’t instantiate an interface?

Marc Gravell
Or indeed read http://msmvps.com/blogs/jon_skeet/archive/2009/07/07/faking-com-to-fool-the-c-compiler.aspx :)
Jon Skeet
+1 for "using alias" - I never knew you could do _that_!
David
+31  A: 

This is one of the most unusual i've seen so far (aside from the ones here of course!):

public class Turtle<T> where T : Turtle<T>
{
}

It lets you declare it but has no real use, since it will always ask you to wrap whatever class you stuff in the center with another Turtle.

[joke] I guess it's turtles all the way down... [/joke]

RCIX
You can create instances, though: class RealTurtle : Turtle<RealTurtle> { } RealTurtle t = new RealTurtle();
Marc Gravell
Indeed. This is the pattern that Java enums use to great effect. I use it in Protocol Buffers too.
Jon Skeet
But you can't do that, seeing as RealTurtle is not a Turtle<RealTurtle>....
RCIX
RCIX, oh yes it is.
Joshua
I stand corrected.
RCIX
I have used this pattern quite a lot in fancy generics stuff. It allows things like a correctly typed clone, or creating instances of itself.
Lucero
I dunno. It may not be illegal, but it sure is one of the strangest things i've seen it let me do.
RCIX
This is the 'curiously recurring template pattern' http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern
Porges
I second Lucero...use it if you want methods on the base class to return the exact type of the inheritor.
flq
This is perfectly valid, and can be very useful to allow the base class to reference the child class's type (for example, [Java Enums](http://madbean.com/2004/mb2004-3/) use it to make sure `compareTo` can only be passed values of that enum). -1...
BlueRaja - Danny Pflughoeft
+13  A: 

I found a second really strange corner case that beats my first one by a long shot.

String.Equals Method (String, String, StringComparison) is not actually side effect free.

I was working on a block of code that had this on a line by itself at the top of some function:

stringvariable1.Equals(stringvariable2, StringComparison.InvariantCultureIgnoreCase);

Removing that line lead to a stack overflow somewhere else in the program.

The code turned out to be installing a handler for what was in essence a BeforeAssemblyLoad event and trying to do

if (assemblyfilename.EndsWith("someparticular.dll", StringComparison.InvariantCultureIgnoreCase))
{
    assemblyfilename = "someparticular_modified.dll";
}

By now I shouldn't have to tell you. Using a culture that hasn't been used before in a string comparison causes an assembly load. InvariantCulture is not an exception to this.

Joshua
I guess "loading an assembly" is a side effect, since you can observe it with BeforeAssemblyLoad!
Jacob
+12  A: 

VB.NET, nullables and the ternary operator:

Dim i As Integer? = If(True, Nothing, 5)

This took me some time to debug, since I expected i to contain Nothing.

What does i really contain? 0.

This is surprising but actually "correct" behavior: Nothing in VB.NET is not exactly the same as null in CLR: Nothing can either mean null or default(T) for a value type T, depending on the context. In the above case, If infers Integer as the common type of Nothing and 5, so, in this case, Nothing means 0.

Heinzi
+6  A: 

PropertyInfo.SetValue() can assign ints to enums, ints to nullable ints, enums to nullable enums, but not ints to nullable enums.

enumProperty.SetValue(obj, 1, null); //works
nullableIntProperty.SetValue(obj, 1, null); //works
nullableEnumProperty.SetValue(obj, MyEnum.Foo, null); //works
nullableEnumProperty.SetValue(obj, 1, null); // throws an exception !!!

Full description here

Anders Ivner
+34  A: 

Assign This!


This is one that I like to ask at parties (which is probably why I don't get invited anymore):

Can you make the following piece of code compile?

public void Foo()
    {
        this = new Teaser();
    }

An easy cheat could be:

string cheat = @"
    public void Foo()
    {
        this = new Teaser();
    }
";

But the real solution is this:

public struct Teaser
{
    public void Foo()
    {
        this = new Teaser();
    }
}

So it's a little know fact that value types (structs) can reassign their this variable.

Omer Mor
C++ classes can do that too... as I discovered somewhat recently, only to be yelled at for actually trying to use it for an optimization :p
Mark
@Mark reassigning `this` in C++ is evil... i cant express how evil it is ...really...If you want to point this to a new object, manually call the destructor of the object, and then call in-place new, but keep in mind even this will fuck everything up, if the object this pointed to was a derived class of the class you just reconstructed...
smerlin
I was using in-place new actually. Just wanted an efficient way to update all fields :)
Mark
This is also a cheat: `//this = new Teaser();` :-)
AndrewJacksonZA
:-)I'd prefer those cheats in my production code, than this reassignment abomination...
Omer Mor
+5  A: 

The following might be general knowledge I was just simply lacking, but eh. Some time ago, we had a bug case which included virtual properties. Abstracting the context a bit, consider the following code, and apply breakpoint to specified area :

class Program
{
    static void Main(string[] args)
    {
        Derived d = new Derived();
        d.Property = "AWESOME";
    }
}

class Base
{
    string _baseProp;
    public virtual string Property 
    { 
        get 
        {
            return "BASE_" + _baseProp;
        }
        set
        {
            _baseProp = value;
            //do work with the base property which might 
            //not be exposed to derived types
            //here
            Console.Out.WriteLine("_baseProp is BASE_" + value.ToString());
        }
    }
}

class Derived : Base
{
    string _prop;
    public override string Property 
    {
        get { return _prop; }
        set 
        { 
            _prop = value; 
            base.Property = value;
        } //<- put a breakpoint here then mouse over BaseProperty, 
          //   and then mouse over the base.Property call inside it.
    }

    public string BaseProperty { get { return base.Property; } private set { } }
}

While in the Derived object context, you can get the same behavior when adding base.Property as a watch, or typing base.Property into the quickwatch.

Took me some time to realize what was going on. In the end I was enlightened by the Quickwatch. When going into the Quickwatch and exploring the Derived object d (or from the object's context, this) and selecting the field base, the edit field on top of the Quickwatch displays the following cast:

((TestProject1.Base)(d))

Which means that if base is replaced as such, the call would be

public string BaseProperty { get { return ((TestProject1.Base)(d)).Property; } private set { } }

for the Watches, Quickwatch and the debugging mouse-over tooltips, and it would then make sense for it to display "AWESOME" instead of "BASE_AWESOME" when considering polymorphism. I'm still unsure why it would transform it into a cast, one hypothesis is that call might not be available from those modules' context, and only callvirt.

Anyhow, that obviously doesn't alter anything in terms of functionality, Derived.BaseProperty will still really return "BASE_AWESOME", and thus this was not the root of our bug at work, simply a confusing component. I did however find it interesting how it could mislead developpers which would be unaware of that fact during their debug sessions, specially if Base is not exposed in your project but rather referenced as a 3rd party DLL, resulting in Devs just saying :

"Oi, wait..what ? omg that DLL is like, ..doing something funny"

Dynami Le Savard
+5  A: 

I'm not sure if you'd say this is a Windows Vista/7 oddity or a .Net oddity but it had me scratching my head for a while.

string filename = @"c:\program files\my folder\test.txt";
System.IO.File.WriteAllText(filename, "Hello world.");
bool exists = System.IO.File.Exists(filename); // returns true;
string text = System.IO.File.ReadAllText(filename); // Returns "Hello world."

In Windows Vista/7 the file will actually be written to C:\Users\<username>\Virtual Store\Program Files\my folder\test.txt

Spencer Ruport
Missing @ on the first line?
devoured elysium
Whoops. Thanks!
Spencer Ruport
This is indeed a vista (not 7, afaik) security enhancement. But the cool thing is that you can read and open the file with the program files path, while if you look there with explorer there is nothing. This one took me almost a day of work @ a customer before I finally found it out.
Henri
It's definitely a Windows 7 thing too. That's what I was using when I ran into it. I understand the reasoning behind it but it was still frustrating to figure out.
Spencer Ruport
In Vista/Win 7 (winXP technically too) apps should write to an AppData folder in Users-folder land, as its technically user data. Applications shouldn't write to programfiles/windows/system32/etc ever, unless they have admin priveledges, and those priveledges should only be there to say upgrade the program/uninstall it/install new feature. BUT! Still don't write to system32/windows/etc :)If you ran that code above as admin (right click > run as admin) it should theoretically write to the program files app folder.
Steve Syfuhs
Sounds like Virtualization - http://crispybit.spaces.live.com/blog/cns!1B71C2122AD43308!134.entry
Colin Newell
+6  A: 

This is the strangest I've encountered by accident:

public class DummyObject
{
    public override string ToString()
    {
        return null;
    }
}

Used as follows:

DummyObject obj = new DummyObject();
Console.WriteLine("The text: " + obj.GetType() + " is " + obj);

Will throw a NullReferenceException. Turns out the multiple additions are compiled by the C# compiler to a call to String.Concat(object[]). Prior to .NET 4, there is a bug in just that overload of Concat where the object is checked for null, but not the result of ToString():

object obj2 = args[i];
string text = (obj2 != null) ? obj2.ToString() : string.Empty;
// if obj2 is non-null, but obj2.ToString() returns null, then text==null
int length = text.Length;

This is a bug by ECMA-334 §14.7.4:

The binary + operator performs string concatenation when one or both operands are of type string. If an operand of string concatenation is null, an empty string is substituted. Otherwise, any non-string operand is converted to its string representation by invoking the virtual ToString method inherited from type object. If ToString returns null, an empty string is substituted.

280Z28
Hmm, but I can imagine this fault as `.ToString` really should never return null, but string.Empty. Nevertheless and error in the framework.
Dykam
+1  A: 

There is something really exciting about C#, the way it handles closures.

Instead of copying the stack variable values to the closure free variable, it does that preprocessor magic wrapping all occurences of the variable into an object and thus moves it out of stack - straight to the heap! :)

I guess, that makes C# even more functionally-complete (or lambda-complete huh)) language than ML itself (which uses stack value copying AFAIK). F# has that feature too, as C# does.

That does bring much delight to me, thank you MS guys!

It's not an oddity or corner case though... but something really unexpected from a stack-based VM language :)

Bubba88
+1  A: 

From a question I asked not long ago:

http://stackoverflow.com/questions/2215745/conditional-operator-cannot-cast-implicitly

Given:

Bool aBoolValue;

Where aBoolValue is assigned either True or False;

The following will not compile:

Byte aByteValue = aBoolValue ? 1 : 0;

But this would:

Int anIntValue = aBoolValue ? 1 : 0;

The answer provided is pretty good too.

MPelletier
although I`ve not test it I`m sure that this will work: Byte aByteValue = aBoolValue ? (Byte)1 :(Byte) 0;Or: Byte aByteValue =(Byte)( aBoolValue ? 1 : 0);
Alex Pacurar
Yes, Alex, that would work. The key is in the implicit casting. `1 : 0` alone will implicitly cast to int, not Byte.
MPelletier
+5  A: 

Have you ever thought the C# compiler could generate invalid CIL? Run this and you'll get a TypeLoadException:

interface I<T> {
  T M(T p);
}
abstract class A<T> : I<T> {
  public abstract T M(T p);
}
abstract class B<T> : A<T>, I<int> {
  public override T M(T p) { return p; }
  public int M(int p) { return p * 2; }
}
class C : B<int> { }

class Program {
  static void Main(string[] args) {
    Console.WriteLine(new C().M(42));
  }
}

I don't know how it fares in the C# 4.0 compiler though.

EDIT: this is the output from my system:

C:\Temp>type Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1 {

  interface I<T> {
    T M(T p);
  }
  abstract class A<T> : I<T> {
    public abstract T M(T p);
  }
  abstract class B<T> : A<T>, I<int> {
    public override T M(T p) { return p; }
    public int M(int p) { return p * 2; }
  }
  class C : B<int> { }

  class Program {
    static void Main(string[] args) {
      Console.WriteLine(new C().M(11));
    }
  }

}
C:\Temp>csc Program.cs
Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.1
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.


C:\Temp>Program

Unhandled Exception: System.TypeLoadException: Could not load type 'ConsoleAppli
cation1.C' from assembly 'Program, Version=0.0.0.0, Culture=neutral, PublicKeyTo
ken=null'.
   at ConsoleApplication1.Program.Main(String[] args)

C:\Temp>peverify Program.exe

Microsoft (R) .NET Framework PE Verifier.  Version  3.5.30729.1
Copyright (c) Microsoft Corporation.  All rights reserved.

[token  0x02000005] Type load failed.
[IL]: Error: [C:\Temp\Program.exe : ConsoleApplication1.Program::Main][offset 0x
00000001] Unable to resolve token.
2 Error(s) Verifying Program.exe

C:\Temp>ver

Microsoft Windows XP [Version 5.1.2600]
Jordão
Works for me with both the C# 3.5 compiler and the C# 4 compiler...
Jon Skeet
In my system, it doesn't work. I'll paste the output in the question.
Jordão
It failed for me in .NET 3.5 (don't have time to test 4.0). And I can replicate the problem with VB.NET code.
Mark Hurd
Works for me in C# 4
Omer Mor
+6  A: 

What if you have a generic class that has methods that could be made ambiguous depending on the type arguments? I ran into this situation recently writing a two-way dictionary. I wanted to write symmetric Get() methods that would return the opposite of whatever argument was passed. Something like this:

class TwoWayRelationship<T1, T2>
{
    public T2 Get(T1 key) { /* ... */ }
    public T1 Get(T2 key) { /* ... */ }
}

All is well good if you make an instance where T1 and T2 are different types:

var r1 = new TwoWayRelationship<int, string>();
r1.Get(1);
r1.Get("a");

But if T1 and T2 are the same (and probably if one was a subclass of another), it's a compiler error:

var r2 = new TwoWayRelationship<int, int>();
r2.Get(1);  // "The call is ambiguous..."

Interestingly, all other methods in the second case are still usable; it's only calls to the now-ambiguous method that causes a compiler error. Interesting case, if a little unlikely and obscure.

tclem
Opponents of method overloading will love this one ^^.
SealedSun
+5  A: 

In an API we're using, methods that return a domain object might return a special "null object". In the implementation of this, the comparison operator and the Equals() method are overridden to return true if it is compared with null.

So a user of this API might have some code like this:

return test != null ? test : GetDefault();

or perhaps a bit more verbose, like this:

if (test == null)
    return GetDefault();
return test;

where GetDefault() is a method returning some default value that we want to use instead of null. The surprise hit me when I was using ReSharper and following it's recommendation to rewrite either of this to the following:

return test ?? GetDefault();

If the test object is a null object returned from the API instead of a proper null, the behavior of the code has now changed, as the null coalescing operator actually checks for null, not running operator= or Equals().

Tor Livar
not really a c# corner case, but dear lord who thought that up?!?
Ray Booysen
Isn't this code just using nullable types? Hence ReSharper recommending the "??" use. As Ray said, I wouldn't have thought this a corner case; or am I wrong?
Tony
Yes, the types are nullable - and there is a NullObject in addition. If it is a corner case, I don't know, but at least it is a case where 'if (a != null) return a; return b;' is not the same as 'return a ?? b'. I absolutely agree it is a problem with the framework/API design - overloading == null to return true on an object is certainly not at good idea!
Tor Livar
A: 

The following doesn't work:

if (something)
    doit();
else
    var v = 1 + 2;

But this works:

if (something)
    doit();
else {
    var v = 1 + 2;
}
Anders Rune Jensen
I don't see how it is a corner case... In the first example, there is no way you can use the `v` variable, since its scope is the `else` block and you can only have one instruction in it if you don't put braces
Thomas Levesque
i don't see the difference of the two code snippet.
Benny
@Thomas: Yes, but why is that an error? I might have wanted to add the statement just to be able to break in the else clause. In C++ this is perfectly valid. I find it discomforting that there is a semantic difference between else {} and else when there is only one statement in the clause.
Anders Rune Jensen
@Anders: Your answers put a lot of focus on the fact that C# differs from C++ like here: http://stackoverflow.com/questions/194484/whats-the-strangest-corner-case-youve-seen-in-c-or-net/1047948#1047948 This thread isn't about the differences between C# and C++. An edge case in C# isn't a difference from C++. Others have noted you will find answers in the C# spec.
John K
@jdk: I added the C++ note for completeness. I agree that it might not be the biggest edge case I have seen, it just suprised me when I found it yesterday.
Anders Rune Jensen
After a bit of reflection I can see that this will help people new to programming and with almost no harm for normal programmers. So instead of calling it a corner case, one might call it a feature :-)
Anders Rune Jensen
+5  A: 

C# Accessibility Puzzler


The following derived class is accessing a private field from its base class, and the compiler silently looks to the other side:

public class Derived : Base
{
    public int BrokenAccess()
    {
        return base.m_basePrivateField;
    }
}

The field is indeed private:

private int m_basePrivateField = 0;

Care to guess how we can make such code compile?

.

.

.

.

.

.

.

Answer


The trick is to declare Derived as an inner class of Base:

public class Base
{
    private int m_basePrivateField = 0;

    public class Derived : Base
    {
        public int BrokenAccess()
        {
            return base.m_basePrivateField;
        }
    }
}

Inner classes are given full access to the outer class members. In this case the inner class also happens to derive from the outer class. This allows us to "break" the encapsulation of private members.

Omer Mor
That actually is well-documented; http://msdn.microsoft.com/en-us/library/ms173120%28VS.80%29.aspx. It can be a useful feature at times, especially if the outer class is static.
Zor
Yes - of course it's documented. However, very few people solved this puzzle so I thought it's a cool piece of trivia.
Omer Mor
Seems like you'd have a very strong possibility of a stack overflow by having an inner class inherit its owner...
jamietre
I don't see why - ?
Omer Mor
+1  A: 

If you have the extension method:

public static bool? ToBoolean(this string s)
{
    bool result;

    if (bool.TryParse(s, out result))
        return result;
    else
        return null;
}

and this code:

string nullStr = null;
var res = nullStr.ToBoolean();

This will not throw an exception because it is an extension method (and really HelperClass.ToBoolean(null)) and not an instance method. This can be confusing.

lasseespeholt
I don't think this is a strange corner-case, more run of the mill syntax design. This behaviour allows you to do things like `static void IfNotNull<T>(Action<T> action)`... If your extension method has a problem with a null `this` parameter then throw an `ArgumentNullException`.
Keith
@Keith It can certainly be useful but when you look at it (from a Java, C++, C# 2 perspective) it will be a strange thing and as a C# 3+ developer you would still have to check whether this is indeed a extension method (not on strings, but on more advanced examples) and not an instance method where they (others code) forgot a null-check.
lasseespeholt
@lasseespeholt - I guess my point is that the extension method way of working is better in all the places where you would use one rather than an instance method. Take your example method: it returns a `bool?` - it's quite acceptable (even preferred) for your `nullStr.ToBoolean()` to return `null`, rather than have it throw a `NullReferenceException`
Keith
A: 

here are a few of mine:

  1. this can be null when calling an instance method with out a NullReferenceException being thrown
  2. a default enumeration value doesn't have to be defined for the enumeration

Simple one first: enum NoZero { Number = 1 }

        public bool ReturnsFalse()
        {
            //The default value is not defined!
            return Enum.IsDefined(typeof (NoZero), default(NoZero));
        }

The below code can actually print true!

 internal sealed class Strange
{
    public void Foo()
    {
        Console.WriteLine(this == null);
    }
}

A simple piece of client code that will result in that is delegate void HelloDelegate(Strange bar);

public class Program
{
    [STAThread()]
    public static void Main(string[] args)
    {
        Strange bar = null;
        var hello = new DynamicMethod("ThisIsNull",
            typeof(void), new[] { typeof(Strange) },
         typeof(Strange).Module);
        ILGenerator il = hello.GetILGenerator(256);
        il.Emit(OpCodes.Ldarg_0);
        var foo = typeof(Strange).GetMethod("Foo");
        il.Emit(OpCodes.Call, foo);
        il.Emit(OpCodes.Ret);
        var print = (HelloDelegate)hello.CreateDelegate(typeof(HelloDelegate));
        print(bar);
        Console.ReadLine();
    }
}

this is actually true in most languages as long as the instance method when called doesn't use the state of the object. this is only dereferenced when the state of the object is accessed

Rune FS
The enum case isn't actually surprising, the default underlaying type of an enum is `int`, so default of the enum will return 0, which is quite undefined in NoZero indeed. Even by specifying a custom type (within `byte`, `sbyte`, `short`, `ushort`, `int`, `uint`, `long`, or `ulong`) to your enum, the default value of all those type is still 0.
Dynami Le Savard
@Dynami yes it's because of the default value of the underlying type but (to me) it's rather senseless to have an invalid default value for a valuetype it's kind of having (1,-1) as default for int. The value simply makes no sense in the context of the given type
Rune FS