What s the strangest corner case you ve seen in C or NET

0 votes

I have a small collection of puzzles and corner cases, and I'm constantly interested in learning more. The page mainly deals with bits and pieces of the C# language, although I also find the fundamentals of.NET interesting. Here's one that isn't on the page but that I think is incredible:

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

Since "new" (when used with a reference type) always creates a new object, I would expect it to print False. It should, according to the specifications for both C# and the CLI. Well, not in this specific instance. On every version of the framework I've tested it with, it prints True. (I must admit that I haven't tried it on Mono.)

Just to be clear, I wasn't specifically searching for a discussion or explanation of this peculiarity; this is merely an example of the kind of stuff I'm looking for. I was really looking for comparable strange behaviour (it's not the same as usual string interning; in particular, string interning doesn't typically happen when a function Object() { [native code] } is invoked.)

Any other gems lurking out there?

Dec 9, 2022 in Android by Edureka
• 13,630 points
449 views

1 answer to this question.

0 votes

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...

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

answered Dec 12, 2022 by Edureka
• 12,690 points

edited Mar 5

Related Questions In Android

0 votes
0 answers

How do I turn a C# object into a JSON string in .NET?

I have classes like these: class MyDate { ...READ MORE

Sep 20, 2022 in Android by Edureka
• 13,630 points
567 views
0 votes
0 answers

What are the differences between C, C# and C++ in terms of real-world applications?

As I stated in an earlier article, ...READ MORE

Sep 20, 2022 in Android by Edureka
• 13,630 points
666 views
0 votes
0 answers

What is the difference between C# and .NET?

May I know what is the difference ...READ MORE

Sep 20, 2022 in Android by Edureka
• 13,630 points
462 views
0 votes
0 answers

Error: No toolchains found in the NDK toolchains folder for ABI with prefix: llvm

I tried to use Gradel (gradlew clean ...READ MORE

Sep 21, 2022 in Android by Edureka
• 13,630 points
574 views
0 votes
1 answer

SQLite.Net not being able to create text file in Win IoT Library!

That exception comes when access limited to ...READ MORE

answered Aug 3, 2018 in IoT (Internet of Things) by nirvana
• 3,090 points
1,269 views
0 votes
2 answers

Is there a .NET equivalent to Apache Hadoop?

Hadoop is a Java-based platform. So, to ...READ MORE

answered Jul 16, 2020 in Big Data Hadoop by Suhana
• 340 points
1,977 views
0 votes
1 answer

Validate String against USPS State Abbreviations

Try something like this: private static String states ...READ MORE

answered Sep 20, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
1,134 views
0 votes
1 answer

Authenticate on an ASP.Net Forms Authorization website from a console app

Essentially, we need to record a regular ...READ MORE

answered Sep 20, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
1,158 views
0 votes
0 answers

Convert VB.NET --> C# Projects

Can someone suggest a suitable program that ...READ MORE

Dec 9, 2022 in Android by Edureka
• 13,630 points
605 views
0 votes
1 answer

Open Contact information in Contact List using Phone Number of Contact Android Studio

You must first obtain the contact's CONTACT ...READ MORE

answered Nov 7, 2022 in Android by Edureka
• 12,690 points
1,285 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP