C # reference text keywords: null, true, false, and default

Source: Internet
Author: User

Null

NullA keyword is a text value that does not reference any null reference of an object.

  1. Null is the default value of the reference type variable.
  2. C #2.0 introducedType that can be nullThis is a data type that can be set to an undefined value. A value that can be null can represent a value in the range of basic values of a general type. When a null value is added, it is equivalent to the union of the common value type and null.

Null typeIs an instance of the System. Nullable structure.

System. Nullable <T>
// Or abbreviated
T?
// The two can be exchanged, and T is the value type.
  1. The value type variable that can be assigned a null value. An empty type based on the reference type cannot be created.
  2. If the base type value is null, use the System. Nullable. GetValueOrDefault attribute to return the value or default value of the base type, for example, int j = x. GetValueOrDefault ();
  3. Use the read-only attributes of HasValue and Value to test whether it is null or retrieve the Value. For example, if (x. HasValue) j = x. Value;
      • If this variable contains a value, the HasValue attribute returns True; or if the value of this variable is null, False is returned.

      • If a Value is assigned, the Value Attribute returns this Value. Otherwise, System. InvalidOperationException is thrown.

      • Set HasValue to false by default for empty type variables. Undefined Value.

  4. Use ?? The default value is assigned to operators. If the null type is assigned to a non-null type, the default value is applied, such as int? X = null; int y = x ?? -1 ;. (For example, in photo album management, if an empty image is uploaded, you can use this formula to assign it a default image)

  5. Nested empty types are not allowed. The following line is not compiled: Nullable <int> n;

  6. True & false

    Can be used as operators and identifiers

    1. If the type defines the true operator, it must also define the false operator.
    2. The type cannot directly overload the conditional logical operators (& |), but the same effect can be achieved by reloading the rule logical operators and the true and false operators.
    Default

    DefaultKeywords can be used in switch statements or generic code.

    1. If no case expression matches the switch value, the control is passed to the statement following the optional default label. If no default tag exists, the control is passed outside the switch.

      • Int id = int32.Parse (Console. ReadLine ());
        Switch (id)
        {
        Case 1:
        Console. WriteLine ("Lee ");
        Break;
        Case 2:
        Console. WriteLine ("Tang ");
        Break;
        Default:
        Console. WriteLine ("Sorry, no one match this ID! ");
        Break;
        }
    2. One problem that occurs in generic classes and generic methods is how to assign the default value to parameterized type T when the following conditions are unknown:
      • T is the reference type or value type.

      • If T is of the value type, whether it is a value or a structure.

      • Public class GenericClass <T>
        {
        Public T dosomething (arg)
        {
        T temp = default (T );

        If (arg! = Null)
        {
        Temp = arg. Favs;
        }
        Return temp;
        }
        //
        }

        Given a variable T of the parameterized type t, the Statement T = null is valid only when t is of the reference type; only when T is of the numerical type rather than the structure, statement t = 0 can be used normally. The solution is to use the default keyword, which returns NULL for the reference type and zero for the value type. For the structure, this keyword returns each structure member whose Initialization is zero or empty, depending on whether the structure is a value type or a reference type.

     

    For more information, see the MSDN document.

    All content in this Blog is provided as "the status quo" without any guarantee and no rights are granted.
    This posting is provided "as is" with no warranties, and confers no rights.

    Related Article

    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.