The this keyword and double question mark (??) confuse me
Possible Duplicate:
What do two question marks together mean in C#?
Is this a new feature added to C# 3.x ?
eg
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 ie
return a == 0 ? a:b;
yes I can understand what this is. Could someone please explain ?
??
- null coalescing operator introduced with .Net 2.0
this
in method -> specifies extension method on existing type, introduced with C# 3.0
这是一个null-coalising操作符,请参阅MSDN解释
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.
上一篇: 这个操作员是什么,“??”
下一篇: 这个关键字和双重问号(??)混淆了我