C # null is an object

When I wrote code in C # a few days ago, I noticed that the compiler complained that I had to cast null to a specific object.

Does this mean that null is just an unassigned version of the type? Or is it a singleton null class value like Java (even if it has special privileges)?

EDIT: example code indicating an error:

 public String duplicate(String toDuplicate) { return toDuplicate + toDuplicate; } public String duplicate(int? toDuplicate) { String asString = toDuplicate.toString(); return asString + asString; } public static int Main(string[] args) { //This needs to be cast: duplicate(null); //To: duplicate((string)null); } 

I commented on null in Java by reading this:

There is also a special null type, the null expression type, which has an unnamed name. Since the null type does not have a name, it is not possible to declare a variable of the null type or of the null type. Null reference is the only possible value of an expression of null type. A null reference can always be passed to any reference type. In practice, the programmer can ignore the null type and just pretend that null is just a special literal, which can be any reference type.

Found here: Is a null object?

+6
source share
6 answers

As per MSDN description :

The null keyword is a literal representing a null reference that does not apply to any object. Null is the default value for variables of a reference type. Normal value types cannot be null. However, NULL value types were introduced in C # 2.0.

+5
source

I get an error when you overload the methods, and the compiler cannot decide which method to call at compile time. This is true?

+6
source

null is a non-core reference for any type. This is not the meaning. null does not have a specific type.

+1
source

No, this is not an object. Null is the default value for variables of a reference type. Normal value types cannot be null. but there is another set called nullable .

+1
source

No - null is just a literal for a null reference.

The reason you need to “throw” it this way (I put the word in quotation marks because you are not actually throwing an instance of something) just helps the compiler to allow overloading of the method you are calling, or the method you are calling even exists.

In your example, you need to indicate that you are calling the "duplicate" method, which takes one string argument. If you omit the cast, the compiler only knows that the method you intended to call is called “duplicate” and has one argument, but it cannot determine what type of argument - you meant the call duplicate(string) or did you want to call duplicate(some other type) ? Should it compile or should it tell you that the method you are trying to call does not exist?

You will also get the same problem if you have a duplicate (byte []), because now your call is ambiguous without an explicit cast.

+1
source

You are probably referring to the following leading to a compiler error:

 int? nullableInt = (somecondition) ? value : null; 

Indeed, you need to add a cast here:

 int? nullableInt = (somecondition) ? value : (int?)null; 

Although I cannot explain this in detail, I suspect the following:

int? actually a short form for a Nullable<int> , so basically it is an instance of an object. When set to int nullableInt Nullable<int> property will be set internally. Direct null assignment will also be fine.

However, a conditional assignment will return two different types: int if somecondition is true and object ( null ) otherwise.

Now the compiler does not know how to handle this, since the ternary operator must return values ​​of the same type. Therefore, you need to specify the desired type for the null value.

Sorry if this is not a very deep technical explanation - I am sure there is someone who can better understand this, but it can help to understand it better.

+1
source

Source: https://habr.com/ru/post/889684/


All Articles