What does the symbol mean ?? after type name

I am using the Eto gui framework . I saw magic grammar in the source code; eg:

int x; int? x; void func(int param); void func(int? param); 

What is the difference? I'm confused. and symbol ? hard to follow google.

+4
source share
2 answers

struct (e.g. int , long , etc.) cannot accept null by default. Thus, .NET provides a generic struct named Nullable<T> , which can be of type T from any other struct s.

 public struct Nullable<T> where T : struct {} 

It provides a bool HasValue property that indicates whether the current Nullable<T> object has a value; and the T Value property, which receives the value of the current value Nullable<T> (if HasValue == true , otherwise it throws an InvalidOperationException ):

 public struct Nullable<T> where T : struct { public bool HasValue { get { /* true if has a value, otherwise false */ } } public T Value { get { if(!HasValue) throw new InvalidOperationException(); return /* returns the value */ } } } 

And finally, in answer to your TypeName? question TypeName? is a shortcut to Nullable<TypeName> .

 int? --> Nullable<int> long? --> Nullable<long> bool? --> Nullable<bool> // and so on 

and in use:

 int a = null; // exception. structs -value types- cannot be null int? a = null; // no problem 

For example, we have a Table class that generates an HTML <table> in a method called Write . Cm:

 public class Table { private readonly int? _width; public Table() { _width = null; // actually, we don't need to set _width to null // but to learning purposes we did. } public Table(int width) { _width = width; } public void Write(OurSampleHtmlWriter writer) { writer.Write("<table"); // We have to check if our Nullable<T> variable has value, before using it: if(_width.HasValue) // if _width has value, we'll write it as a html attribute in table tag writer.WriteFormat(" style=\"width: {0}px;\">"); else // otherwise, we just close the table tag writer.Write(">"); writer.Write("</table>"); } } 

Using the above class - as an example - is something like this:

 var output = new OurSampleHtmlWriter(); // this is NOT a real class, just an example var table1 = new Table(); table1.Write(output); var table2 = new Table(500); table2.Write(output); 

And we will have:

 // output1: <table></table> // output2: <table style="width: 500px;"></table> 
+5
source

This means that they are Nullable , can contain null values .

if you have identified:

 int x; 

then you cannot do:

 x = null; // this will be an error. 

but if you defined x as:

 int? x; 

then you can do:

 x = null; 

Nullable<T> Structure

In C # and Visual Basic, do you mark the value type as NULL using? notation after value type. For example, int? in C # or Integer? in Visual Basic, declares an integer value type that can be set to null.

Personally, I used http://www.SymbolHound.com to search with characters, looked at the result here

? is just syntactic sugar, its equivalent:

int? x int? x matches Nullable<int> x

+8
source

All Articles