Results 1 to 7 of 7

Thread: What is the meaning of "??" symbol in C#.NET?

  1. #1
    Join Date
    Nov 2009
    Posts
    51

    What is the meaning of "??" symbol in C#.NET?

    Hello friends,
    I am recently start learning C# language. In one book I have seen one line of code which states -
    Code:
    return (strs ?? string.Empty).Replace(txtFinds.Text, txtReplaces.Text);
    I don't understand above line code. I don't understand exact meaning of (strs ?? string.Empty) this line. Can anyone tell me What is the meaning of "??" symbol in C#.NET?
    Thank you.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: What is the meaning of "??" symbol in C#.NET?

    Hey it is called as null coalescing operator. This is used to returns the first argument if it's value is non null and returns the second argument if it's null. In your code (str ?? string.Empty) is normally used to assign null strings for empty strings. It can also be useful with null-able types like as follows:
    Code:
    int? nullableInts = GetNullableInts();
    int normalInts = nullableInts ?? 0;

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: What is the meaning of "??" symbol in C#.NET?

    as per my information it is known as null coalescing operator. It is very useful operator in c# language. It allow you to conditionally select first non-null value from a series of values. Just look at following example:
    Code:
    string names = null;
    string nicknames = GetNickname();
    string results = names ?? nicknames ?? "<default>";

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: What is the meaning of "??" symbol in C#.NET?

    Hey it is exactly equivalent to ternary operator of java. It means that you can write (str == null ? string.Empty : str) instead of (strs ?? string.Empty). Isn't it very simple. It is called as null coalescing operator. This very easy to use and implement, but if you add an arbitrary expression for strs then it will becomes more complicated, because the expression of such kind would only be evaluated once.

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: What is the meaning of "??" symbol in C#.NET?

    I think it is called as null coalescing operator. I think it is very difficult to understand. If yo want to write small line of code then you can use this operator. You can write (strs ?? String.Empty) in following tow alternative ways.
    Code:
    if (strs == null) {
        return String.Empty;
    } else {
        return strs;
    }
    or as a ternary statement:
    Code:
    strs == null ? strs : String.Empty;

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: What is the meaning of "??" symbol in C#.NET?

    Then "??" operator tells that return me the non null value. This is called as null coalescing operator. This is used to return non null value. If you don't understand the use following Example.
    Code:
    string lastNames = null; 
    
    string personNames = lastNames ?? "mukesh bogle";
    The above code will return "mukesh bogle" since lastNames value is null.

  7. #7
    Join Date
    Jan 2012
    Posts
    1

    Re: What is the meaning of "??" symbol in C#.NET?

    Can any one please tell me what dose "!= null" means, i am new in C# and i cant figure out the meaning..

Similar Threads

  1. Replies: 2
    Last Post: 24-02-2012, 03:50 PM
  2. What is the meaning of "64 bit" version of Windows 7?
    By McKenzie! in forum Operating Systems
    Replies: 10
    Last Post: 15-09-2011, 11:50 PM
  3. Internet Explorer 9: Meaning of "file isn't commonly downloaded"
    By Kanwaljeet in forum Technology & Internet
    Replies: 4
    Last Post: 16-03-2011, 08:22 PM
  4. Error while compiling: javac= "cannot find symbol"
    By Harper 21 in forum Software Development
    Replies: 5
    Last Post: 07-01-2010, 08:58 AM
  5. "On Click, Line 1: unexpected symbol near" error
    By Gopesh in forum Operating Systems
    Replies: 3
    Last Post: 10-03-2009, 05:41 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,628,038.65531 seconds with 17 queries