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 are the controllable types of C#

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what are the controllable types of C#". In the daily operation, I believe that many people have doubts about the controllable types of C#. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what are the controllable types of C#?" Next, please follow the editor to study!

C # single question mark? And double question mark: single question mark is used to assign null to data types such as int,double,bool that cannot be directly assigned to null, meaning that the data type is of Nullable type.

Int? I = 3

Equivalent to:

Nullable I = new Nullable (3); int I; / / default 0 int? Ii; / / default null

Double question marks can be used to determine that a variable returns a specified value when it is null.

Next, we will explain in detail.

C # nullable type (Nullable)

C # provides a special data type, the nullable type (nullable type), which can represent a value within the normal range of its underlying value type, plus a null value.

For example, Nullable

< Int32 >

Pronounced "nullable Int32" and can be assigned to any value between-2147483648 and 2147483647, or to a null value Similarly, Nullable

< bool >

Variables can be assigned as true or false or null.

The ability to assign null to numeric or Boolean types is particularly useful when working with databases and other data types that contain elements that may not be assigned. For example, a Boolean field in a database can store the value true or false, or it can be undefined.

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

< data_type>

? = null

The following example demonstrates the use of nullable data types:

Example

Using System; namespace CalculatorApplication {class NullablesAtShow {static void Main (string [] args) {int? Num1 = null; int? Num2 = 45; double? Num3 = new double (); double? Num4 = 3.14157; bool? Boolval = new bool? (); / / display values Console.WriteLine ("Show values of nullable types: {0}, {1}, {2}, {3}", num1, num2, num3, num4); Console.WriteLine ("a nullable Boolean value: {0}", boolval); Console.ReadLine () }}}

When the above code is compiled and executed, it produces the following results:

Displays the value of the nullable type: 45, 3.14157 A nullable Boolean value:

Null merge operator (?)

The Null merge operator is used to define default values for nullable and reference types. The Null merge operator defines a default value for type conversions in case the value of a nullable type is Null. The Null merge operator implicitly converts an Operand type to an Operand of another nullable (or non-nullable) value type.

If the value of the first Operand is null, the operator returns the value of the second Operand, otherwise it returns the value of the first Operand. The following example demonstrates this:

Example

Using System; namespace CalculatorApplication {class NullablesAtShow {static void Main (string [] args) {double? Num1 = null; double? Num2 = 3.14157; double num3; num3 = num1? 5.34; / / num1 returns 5.34 Console.WriteLine if null ("value of num3: {0}", num3); num3 = num2? 5.34; Console.WriteLine ("value of num3: {0}", num3); Console.ReadLine ();}

When the above code is compiled and executed, it produces the following results:

Num3 value: 5.34 num3 value: 3.14157 at this point, the study of "what are the controllable types of C #" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report