Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 53313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:02:38+00:00 2026-05-10T17:02:38+00:00

I collect a few corner cases and brain teasers and would always like to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:02:38+00:00Added an answer on May 10, 2026 at 5:02 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few iphone and andriod apps and would like to collect better
Few of us would deny the awesomeness of debuggers, but to make it more
I am sending an email and it takes a few seconds to collect data,
I have an iPhone app consisting of a few forms in which I collect
I have few grids on a page. After I edit data in them I
There are 3+ apps which collect parameters in the form of one string, like
Brain totally not working today. So, I have a few columns in a table
ok so i collect the variable below through javascript. var frameState = $(this).attr('id'); I
I added google-collect-1.0.jar to my Android project and it made a 50K .apk into
I am attempting to collect an IEnumerable<Item> from a list of projects. Here's the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.