0

Possible Duplicate:
What do two question marks together mean in C#?

Is this a new feature added to C# 3.x ?

e.g

public class HttpRequests
{
    public string GetHtmlContent(this HttpRequest myRequest)
    {            
       //do something
       return retStr ?? (retStr=new GetHtmlStr(urlStr));
    }
}

The this and ?? are strange to me since I have not updated my know of C# for years. I know C# 2.x.

For conditional if and return value i.e

return a == 0 ? a:b;

yes I can understand what this is. Could someone please explain ?

Community
  • 1
  • 1
  • 5
    http://msdn.microsoft.com/en-gb/library/bb383977.aspx – Oliver Charlesworth Jan 28 '13 at 10:47
  • 5
    http://msdn.microsoft.com/en-us/library/ms173224.aspx – Oliver Charlesworth Jan 28 '13 at 10:48
  • and this http://msdn.microsoft.com/en-us/library/ms173224.aspx – Mediator Jan 28 '13 at 10:48
  • three question in one post – Mediator Jan 28 '13 at 10:49
  • The code you've given invalid. Extension methods (the `this` part) have to be on static methods, in static classes. And of course you haven't declared `retStr` anywhere. – Jon Skeet Jan 28 '13 at 10:50
  • Also: [What are extension methods?](http://stackoverflow.com/questions/403539/what-are-extension-methods), [What do two question marks together mean in C#](http://stackoverflow.com/questions/446835/what-do-two-question-marks-together-mean-in-c) and [Question mark and colon mean in statement? what does it mean?](http://stackoverflow.com/questions/6957214/question-mark-and-colon-mean-in-statement-what-does-it-mean). – poke Jan 28 '13 at 10:53

6 Answers6

3

?? - null coalescing operator introduced with .Net 2.0

this in method -> specifies extension method on existing type, introduced with C# 3.0

Habib
  • 219,104
  • 29
  • 407
  • 436
1

this refers to Extension method

Regarding Extension method you can find a comprehensive detail at this link


The ?? operator is called the null-coalescing operator and is used to define a default value for a nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand.

Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
  • Sorry, but the last part is entirely wrong. You're doing a comparison. You probably meant to assign a value, but that would be also wrong, coalesce does not assign any value. So "a ?? b" would be correctly translated to "a != null ? a : b", and "a ?? b ?? c" to "a != null ? a : (b != null ? b : c))". – JustAnotherUserYouMayKnow Jan 28 '13 at 10:54
  • @JustAnotherUserYouMayKnow - thnx edited!. – Parimal Raj Jan 28 '13 at 10:56
0

This is a null-coalising operator, see MSDN explanation

alex555
  • 1,676
  • 4
  • 27
  • 45
0
GetHtmlContent(this HttpRequest myRequest)

Well this infers it to be an Extension method but does your code compile since your class is not static

When the first parameter of a method includes the this modifier, that method is said to be an extension method. Extension methods can only be declared in non-generic, non-nested static classes. The first parameter of an extension method can have no modifiers other than this, and the parameter type cannot be a pointer type.

Also ?? is a null coalescing operator

string something = maybenull ?? "I cannot be null";

so when maybenull object is null you get the other string assigned to your string.

V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
0

This Qualifier

When using this qualifier before the first parameter in a static method inside a static class, the method is called "extension method". Basically you are saying to the compiler: "hey, I'd like to add this method to an existing class without modifying it".

LINQ makes use of them intensively (see Enumerable Class for more details).

?? or Null-Coalescing Operator

?? or Null-Coalescing Operator is a syntactic sugar for:

var2 = var1 == null ? something : var1;

You can, in facts, replace the above operation with a simple line of code:

var2 = var1 ?? something;
as-cii
  • 12,819
  • 4
  • 41
  • 43
  • 1
    Sorry, but your part about the coalescing operator is wrong. The coalesce operator does NOT assign a value! So "var2 = something" is WRONG. "a ?? b" would be correctly translated to "a != null ? a : b", and "a ?? b ?? c" to "a != null ? a : (b != null ? b : c))". – JustAnotherUserYouMayKnow Jan 28 '13 at 10:56
  • @AS-CII It’s still wrong because when `var1` is not null, `var1` will be assigned to `var2`. And that is not the case in your first code example. – poke Jan 28 '13 at 10:58
  • Sorry for the mistake :) – as-cii Jan 28 '13 at 10:58
0

Yeah, it's new features.

this before the first method parameter means that the method is a extension method. I'm pretty sure the function needs to be static to be valid.

So to use that method, you could write

HttpRequest request;
// assign request
request.GetHtmlContent();

And it would call GetHtmlContent with request as the parameter.

The ?? is the null-coalescing operator, and is used to simplify a check for null.

Instead of

string s;
// ... s something something
// return s == null ? s = "default" : s;

which means that if the string s is null, return this value instead, but if it's not null, return the value of s, you can simply write:

return s ?? "default";

Which returns s if it's not null, and default if s is null.

AkselK
  • 2,563
  • 21
  • 39