Ylbtech-languagesamples-nullable (nullable type)

Source: Internet
Author: User

Ylbtech-microsoft-csharpsamples:ylbtech-languagesamples-nullable (nullable type)

1.A, example (sample) back to top
"Nullable type" sample

This example demonstrates how to use a nullable type. This feature allows a value type to be set to an uninitialized state (that is, an empty state), similar to how reference types are set to null .

The Security Note provides this code example to illustrate a concept that does not represent the most secure coding practice, so you should not use this code example in an application or Web site. Microsoft assumes no responsibility for incidental or consequential damages arising from the use of this code sample for other purposes.

To build and run the nullable types sample in Visual Studio
    1. In Windows Explorer, double-click the Nullable.sln solution file, or click Open on the File menu to open the file.

    2. On the Debug menu, click Start Without Debugging.

To build and run the nullable type sample from the command line
    1. Use the "Change Directory (CD)" command to go to the "Nullable" directory.

    2. Type the following command:

      CD Basicscsc basics.csbasics
    3. Type the following command:
      Cd.. \BOXINGCSC boxing.csboxing
    4. Type the following command:
      Cd.. \OPERATORCSC Operator.csoperator
1.B, sample code back to top

1.b.1, Basics.cs

//Copyright (C) Microsoft Corporation. All rights reserved. //This code is published in compliance with//Microsoft Public License (MS-PL,http://opensource.org/licenses/ms-pl.html) of the terms. ////Copyright (C) Microsoft Corporation. All rights reserved. usingSystem;classnullablebasics{Static voidDisplayvalue (int?num) {        if(Num. HasValue = =true) {Console.WriteLine ("num ="+num); }        Else{Console.WriteLine ("num = null"); }        //if Num. HasValue is false, then Num. Value throws InvalidOperationException        Try{Console.WriteLine ("value = {0}", Num.        Value); }        Catch(InvalidOperationException e) {Console.WriteLine (e.message); }    }    Static voidMain () {Displayvalue (1); Displayvalue (NULL); }}
View Code

1.b.2,

1.B, sample code 2 (sample codes) back to top

1.b.1, Boxing.cs

//Copyright (C) Microsoft Corporation. All rights reserved. //This code is published in compliance with//Microsoft Public License (MS-PL,http://opensource.org/licenses/ms-pl.html) of the terms. ////Copyright (C) Microsoft Corporation. All rights reserved. usingSystem;classnullableboxing{Static voidMain () {int?A; ObjectOA; //assigns a value of nullable<int> (value = default (int), HasValue = False). A =NULL; //assign the value null (because X==null) to OA instead of the boxed "int?". OA =A; Console.WriteLine ("testing ' a ' and ' boxed a ' for null ..."); //You can compare a nullable variable with NULL.         if(A = =NULL) {Console.WriteLine ("a = = null"); }        //you can compare a boxed nullable variable with null .//because the nullable variables of the hasvalue==false can be boxed//the reference is set to NULL.         if(OA = =NULL) {Console.WriteLine ("OA = = NULL"); } Console.WriteLine ("unboxing a nullable type ..."); int? b =Ten; ObjectOB =b; //boxed nullable types can be unboxed        int? Unboxedb = (int?) OB; Console.WriteLine ("b={0}, unboxedb={0}", B, unboxedb); //if it is unboxed to a nullable type, you can set the nullable type to NULL//to cancel boxing.         int? Unboxeda = (int?) OA; if(OA = =NULL&& Unboxeda = =NULL) {Console.WriteLine ("A and Unboxeda are null"); } Console.WriteLine ("attempting to unbox into Non-nullable type ..."); //Nullable type that is set to NULL if it is unboxed as a non-nullable type//Unboxing throws an exception.         Try        {            intUnBoxedA2 = (int) OA; }        Catch(Exception e) {Console.WriteLine ("{0}", E.message); }    }}
View Code

1.b.2,

1.B, sample code 3 (sample codes) back to top

1.b.1, Operator.cs

//Copyright (C) Microsoft Corporation. All rights reserved. //This code is published in compliance with//Microsoft Public License (MS-PL,http://opensource.org/licenses/ms-pl.html) of the terms. ////Copyright (C) Microsoft Corporation. All rights reserved. usingSystem;classnullableoperator{Static int?Getnullableint () {return NULL; }    Static stringGetStringValue () {return NULL; }    Static voidMain () {//??? operator example.         int? x =NULL; //y = x, only if x is null, y =-1.         inty = x?? -1; Console.WriteLine ("y = ="+y); //assigns the return value of the method to I,//only if the return value is null.//assigns the default int value to I.         inti = Getnullableint ()??default(int); Console.WriteLine ("i = ="+i); //?? also applies to reference types.         strings =GetStringValue (); //Displays the contents of S, only if S is null,//"Unspecified" is displayed. Console.WriteLine ("s = {0}", S??"NULL"); }}
View Code

1.b.2,

1.C, (free Download) back to top

Ylbtech
Source: http://ylbtech.cnblogs.com/
This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

Ylbtech-languagesamples-nullable (nullable type)

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.