I am using Eto gui framework. I saw some magic grammar in their source code; for example:
int x;
int? x;
void func(int param);
void func(int? param);
What's different? I am confused.
and the symbol ?
is hard to google.
It means they are Nullable, they can hold null values.
if you have defined:
int x;
then you can't do:
x = null; // this will be an error.
but if you have defined x
as:
int? x;
then you can do:
x = null;
Nullable<T> Structure
In C# and Visual Basic, you mark a value type as nullable by using the ? notation after the value type. For example, int? in C# or Integer? in Visual Basic declares an integer value type that can be assigned null.
Personally I would use http://www.SymbolHound.com for searching with symbols, look at the result here
?
is just syntactic sugar, its equivalent to:
int? x
is same as Nullable<int> x
struct
s (like int
, long
, etc) cannot accept null
by default. So, .NET provides a generic struct
named Nullable<T>
that the T
type-param can be 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 a T Value
property that gets the value of the current Nullable<T>
value (if HasValue == true
, otherwise it will throw 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 of your question, TypeName?
is a shortcut of Nullable<TypeName>
.
int? --> Nullable<int>
long? --> Nullable<long>
bool? --> Nullable<bool>
// and so on
and in usage:
int a = null; // exception. structs -value types- cannot be null
int? a = null; // no problem
For example, we have a Table
class that generates HTML <table>
tag in a method named Write
. See:
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>");
}
}
Usage of the above class -just as an example- is something like these:
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With