Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is a nullable type?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

We often encounter nullable types in projects, so what exactly are nullable types? Next we will analyze it for you from four aspects.

1. Basic knowledge of nullable types

As the name implies, a nullable type means that an object type can be null and is an instance of a System.Nullable structure. A nullable type can represent a normal range of values for the underlying value type, followed by a null value. Of course, it can also be assigned a null value. For example, Nullable can be assigned to true, false, or null.

The standard statement to declare a nullable type is as follows:

T? MyNullableNum = rangedNum/new T? () or null

For example, we declare a nullable type and assign a value of null

Int? IntClass = null

Or use the

Int? IntClass = new int? ()

At the same time, we all know that a nullable type is an instance of a generic Nullable, declared as follows:

Nullable myNullableClass = new Nullable ()

As you can see from the declaration, its underlying type T is any value type that can include struct and enum, but it cannot be a reference type or a nullable type; from this we can see that types may not be nested.

In a nullable type, it contains the following instance members:

The first is HasValue, which is a read-only property that determines whether an object has a value. Returns true if the current value is empty, otherwise returns flase; followed by Value, which is also a read-only property that gets the value of the object. If the current value is not empty, you can get the corresponding value normally, otherwise an exception InvalidOperationException; will be thrown followed by the instance method of GetValueOrDefault (). This method gets the current information through the HasValue and Value attributes. If the current value is not empty, that is, HasValue is true, the value of Value is returned; otherwise, the default value of T type, that is, the default value of private field value, is returned. The last one is the GetValueOrDefault () instance method. This method still gets the current information through the HasValue and Value properties. If the current value is not empty, that is, HasValue is true, the value of Value is returned, otherwise the default value defaultValue is returned.

2. The use of nullable types

Among the nullable types, it is common to use single question marks. And a double question mark? To set up. Among them? It is often used for data types such as int,double,bool that cannot be assigned to null directly; it means that the data type is a Nullable type.

Int? Kend10

Equivalent to

Nullable k = new Nullable (10)

Int m; / / default is 0

Int? M; / / default is null

And?? It is mainly used for the specified value returned when the value of the object is determined to be null.

Int? Num = null

Var k = = num? 9

For a nullable type, it belongs to a special data type that represents a value within the normal range of the corresponding underlying value type, plus a null value.

For example, an object of type int can be assigned any value between-2147483648 and 2147483647, or it can be assigned a null value. For a value of type bool, it can be true, false, or null.

The syntax for declaring a nullable type (controllable type) is as follows:

? = null

The following example illustrates:

Using System

Namespace ExampleTest

{

Class Program {static void Main (string [] args) {int? ObjectNull = null; int? K = 80; double? Db1 = new double (); double? Db2 = 4.56; bool? B = new bool? (); Console.WriteLine ($"Show nullable types {objectNull}, {k}, {db1}, {db2}"); Console.WriteLine ($"a nullable Boolean value: {b}"); Console.ReadLine ();}}

}

And for?? Which is mainly used to define default values for nullable and reference types During its operation, a default value is defined for the type conversion to prevent the nullable type from being null.

Using System

Namespace ExampleTest

{

Class Program {static void Main (string [] args) {double? ObjNull = null; double? K = 4.85; double db; db = objNull? 9.82; Console.WriteLine ($"num3 value: {db}"); db = k? 6.02; Console.WriteLine ($"num3 value: {db}"); Console.ReadLine ();}}

}

3. Case use of nullable types

Look at the use of nullable types through a simple front-end query function.

Page html

Name: gender:-- Please choose-- male and female

Pass in the parameter Dto

Using System

Using System.Collections.Generic

Using System.Linq

Using System.Web

Namespace WebTest.Models

{

Public class TestInDto {/ quarantine point / public int? Kum {get; set;} / name / public string Name {get; set;}}

}

Controller

Using System

Using System.Collections.Generic

Using System.Linq

Using System.Web

Using System.Web.Mvc

Using WebTest.Models

Namespace WebTest.Controllers

{

Public class TestController: Controller {/ / GET: Test public ActionResult Index () {return View ();} public ActionResult TestFunction (TestInDto testInDto) {if (! testInDto.Kum.HasValue) return Json (new {Success = false, Message = "Please choose gender", Data = ""}); int? Num = null; DateTime? Dt = null; bool? B = null; var data = new TestOutDto {Name = testInDto.Name, Num = num? 10, Dt = dt? DateTime.Now, IsExist = b?? False}; return Json (new {Success = true, Message = "", Data = data});}}

}

4. Summary

Finally, nullable types are often used in daily projects, and methods for each logical layer may use nullable types for processing and conversion of incoming and return parameters.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report