[You must know. NET] The 21st time: Understanding full NULL

Source: Internet
Author: User
Tags valid

1 starting from what is NULL?

NULL, a respectable data ID.

In general, NULL represents an empty type, which means nothing, but "nothing" does not mean "nothing". In fact, NULL is so important that in JavaScript, a null type is one of 5 basic primitive types, with undefined, Boolean, number, and string. This importance is also reflected in. NET, but it must be clarified that NULL is not equal to 0, "", String. Empty the concept of "0" values in the usual sense. Instead, NULL has a real meaning, which is a state used to identify a reference to a variable, which means that no object instance is referenced, meaning "nothing," not an object instance, nor a user instance, but a null reference.

In the aforementioned statement, which makes me so clumsy, the central idea is to clarify an inability to speak about the meaning of NULL. What is the practical meaning of NULL in net?

In. NET, null indicates that an object reference is invalid. As the default value for a reference type variable, NULL is for pointers (references), it is the exclusive concept of a reference type variable, representing a state in which a reference type variable is declared but uninitialized, for example:

Object obj = null; When obj is merely a reference pointer on the thread stack, does not mean anything, obj does not point to any valid instances and is initialized to null by default.

What is the difference between object obj and object obj = null?

So is the actual difference between object obj and object obj = null? The answer is: yes. It is mainly embodied in the examination of the compiler. By default, when you create a reference type variable, the CLR initializes it to null, indicating that it does not point to any valid instances, so essentially the two represent the same meaning, but there are differences:

// Copyright  : www.anytao.com

// Author   : Anytao,http://www.anytao.com

// Release   : 2008/07/31 1.0

//编译器检测错误:使用未赋值变量obj

//object obj;
//编译器理解为执行了初始化操作,所以不引发编译时错误
object obj = null;
if (obj == null)

{

 //运行时抛出NullReferenceException异常

 Console.WriteLine(obj.ToString());
}

Note: When I put this question to a few friends, the idea of this has not formed a unified consensus, several comrades have their own understanding, and each has a reason. Of course, I also carefully discussed and analyzed this, but did not form a complete 100% certainty of the answer. However, in the understanding of my more inclined to their own analysis and judgment, so in the above conclusions on the basis of this small thinking will be left to discuss, good thinking and analysis do not forget to leave to everyone. In fact, the

static void Main(string[] args)

{

 object o;

 object obj = null;
}

When you decompile to IL, there is a difference in the IL layer:

.method private hidebysig static void Main(string[] args) cil managed

{

   .entrypoint

   .maxstack 1

   .locals init (

     [0] object o,

     [1] object obj)

   L_0000: nop

   L_0001: ldnull

   L_0002: stloc.1

   L_0003: ret
}

The former does not take any additional action, while the latter pushes a null reference through the ldnull instruction to evaluation stack, while stloc saves the null reference.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.