C # Nullable types

Source: Internet
Author: User

A nullable type assigns the value of its underlying type to its normal range and a null value.

For example,:nullable<int32> can be assigned a value of 2147483618 to 2147483618 and null. (Int?=null or int?=9)

The ability to assign null to a numeric type or Boolean is particularly useful when working with databases and other data types that contain elements that may not be assigned a value .

For example, a Boolean field in a database can store values.

Example Description:

Using System;namespace calculatorapplication{    class nullablesatshow    {        static void Main (string[] args)        {            int? num1 = null;            Int? num2 =;            Double? num3 = new double? ();            Double? NUM4 = 3.14157;            bool? Boolval = new bool? ();            Console.WriteLine ("Show Values for nullable types: {0}, {1}, {2}, {3}",                               num1, num2, num3, num4);            Console.WriteLine ("A nullable Boolean value: {0}", boolval);            Console.ReadLine ();}}}    
Null merge operator (??)

If the value of the first operand is null, the operator returns the value of the second operand, otherwise the value of the first operand is returned.


Using System;namespace calculatorapplication{    class nullablesatshow    {        static void Main (string[] args)        {            double? num1 = null;            Double? num2 = 3.14157;            Double num3;            num3 = Num1?? 5.34;            Console.WriteLine ("Value of num3: {0}", num3);            num3 = num2?? 5.34;            Console.WriteLine ("Value of num3: {0}", num3);            Console.ReadLine ();}}}    






C # Nullable types

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.