What's the strangest corner case you've seen in C# or .NET? -


i collect few corner cases , brain teasers , hear more. page covers c# language bits , bobs, find core .net things interesting too. example, here's 1 isn't on page, 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 print false - after all, "new" (with reference type) always creates new object, doesn't it? specs both c# , cli indicate should. well, not in particular case. prints true, , has done on every version of framework i've tested with. (i haven't tried on mono, admittedly...)

just clear, example of kind of thing i'm looking - wasn't particularly looking discussion/explanation of oddity. (it's not same normal string interning; in particular, string interning doesn't happen when constructor called.) asking similar odd behaviour.

any other gems lurking out there?

i think showed 1 before, fun here - took debugging track down! (the original code more complex , subtle...)

    static void foo<t>() 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          // looks object , smells object...          // throws nullreferenceexception...         console.writeline(t.gettype());     } 

so t...

answer: nullable<t> - such int?. methods overridden, except gettype() can't be; cast (boxed) object (and hence null) call object.gettype()... calls on null ;-p


update: plot thickens... ayende rahien threw down similar challenge on blog, where t : class, new():

private static void main() {     canthishappen<myfunnytype>(); }  public static void canthishappen<t>() t : class, new() {     var instance = new t(); // new() on ref-type; should non-null,     debug.assert(instance != null, "how did break clr?"); } 

but can defeated! using same indirection used things remoting; warning - following pure evil:

class myfunnyproxyattribute : proxyattribute {     public override marshalbyrefobject createinstance(type servertype) {         return null;     } } [myfunnyproxy] class myfunnytype : contextboundobject { } 

with in place, new() call redirected proxy (myfunnyproxyattribute), returns null. go , wash eyes!


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -