Skip to main content

Post 2: C# for Java: ?? Operator in C#

?? Operator (C# Reference)

The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand.

A nullable type can represent a value from the type’s domain, or the value can be undefined (in which case the value is null). You can use the ?? operator’s syntactic expressiveness to return an appropriate value (the right hand operand) when the left operand has a nullible type whose value is null. If you try to assign a nullable value type to a non-nullable value type without using the ?? operator, you will generate a compile-time error. If you use a cast, and the nullable value type is currently undefined, an InvalidOperationException exception will be thrown.

The result of a ?? operator is not considered to be a constant even if both its arguments are constants.

class NullCoalesce
{
    static int GetNullableInt()
    {
        return null;
    }

    static string GetStringValue()
    {
        return null;
    }

    static void Main()
    {
        int? x = null;

        // Set y to the value of x if x is NOT null; otherwise, 
        // if x = null, set y to -1. 
        int y = x ?? -1;

        // Assign i to return value of the method if the method's result 
        // is NOT null; otherwise, if the result is null, set i to the 
        // default value of int. 
        int i = GetNullableInt() ?? default(int);

        string s = GetStringValue();
        // Display the value of s if s is NOT null; otherwise,  
        // display the string "Unspecified".
        Console.WriteLine(s ?? "Unspecified");
    }
}

Comments

Popular posts from this blog

Index: JSF

Java Server Faces Basics Post 1: JSF : Introduction to JSF  Post 2: JSF:  configuration files  Post 3: JSF: Installation Post 4: JSF: Your first JSF project   Standard JSF Tags Post 1: JSF: textbox example Post 2: JSF: password example Post 3: JSF: textarea example JSF 2 GraphicImage Example Post 4: JSF: GraphicImage Example Post 101 : JSF : The valueChangeListener Attribute Example

Index: iReport

iReport for Java ------------------------------------------------------------ Titles and Headers ------------------------------------------------------------ Post 1: iReport : Downloading, installing, and running JasperReports and iReport Post 2: iReport : Creating your first "Hello World" report   Post 3: iReport : Understanding iReport Elements Post 4: iReport : Creating and sizing the title for your report Post 5: iReport: Inserting a company logo in the title of your report  Post 6: iReport : Installation of PostgreSQL Post 7: iReport : Creating a report from relational data Post 8: iReport : Using dynamic titles that can change during report processing. Post 9: iReport : Adding a simple header to your report Post 10: iReport : Setting margins for your report and aligning the report header relative to report margins  ------------------------------------------------------------ Body and Footer ---------------------------------------------------------...