c#值为null怎么解决_C#编程语⾔Null参考异常和解决⽅案
c#值为null怎么解决
Null Reference Exception is one of the most occurring exception. Object oriented generally creates new object for the variables. But if it is not created and not set by the developer trying to access an variable object will cause Null Reference Exception. In this examples we will use Csharp programming language for Null Reference Exception.
空引⽤异常是最常发⽣的异常之⼀。 ⾯向对象的通常会为变量创建新的对象。 但是,如果未创建它并且开发者未设置它试图访问变量对象,则将导致Null Reference Exception。 在此⽰例中,我们将为空引⽤异常使⽤Csharp编程语⾔。
引发Null参考异常 (Raise Null Reference Exception)
We can generate Null Reference Exception with the following example . In this code we create a variable named name and set its value t null. If we try to run function ToUpper() which will return the name variable string in uppercase will create an Exception because there is no such value to use.
我们可以使⽤以下⽰例⽣成Null Reference Exception。 在此代码中,我们创建⼀个名为name的变量并将其值设置为null。 如果我们尝试运⾏函数ToUpper() ,该函数将以⼤写形式返回name变量字符串,则会创建⼀个Exception,因为没有可使⽤的此类值。
variable怎么记string name=null;
name.ToUpper();
Another example can be a class or struct which is not set into an instance. This example will be similar to the previous example. We will create a variable ferrari which is a Car class type.
另⼀个⽰例可以是未设置为实例的类或结构。 此⽰例将类似于前⾯的⽰例。 我们将创建⼀个可变的ferrari ,它是Car类类型。
Car ferrari;
ferrari.Start();
解决Null参考异常 (Solve Null Reference Exception)
This code snippet will cause Reference Exception. To remove Null Reference Exception initialize the name object. We simply set some value different than null to prevent Null Reference Exception.
此代码段将导致引⽤异常。 要删除Null引⽤异常,请初始化名称对象。 我们只是简单地设置⼀些不同于null的值来防⽌Null Reference Exception。
string name="This will not throw Null Reference Exception";
name.ToUpper();
了解更多C Printf()函数教程,并提供⽰例c#值为null怎么解决