This question already has answers here:
Nullable integer in .NET
(6 answers)
Variable type ending with ?
(7 answers)
Variable = null in method declaration? What does it mean and how is this method called?
(1 answer)
What does the bool? return type mean?
(3 answers)
I don't understand parameter declaration
(3 answers)
Closed 3 years ago.
Hey i can't seem to find the answer to this question answered in a way that makes complete sense to me.
when declaring nullable arguments in c#, what is the difference between declaring a argument with a question mark like this:
public method(int? variable):
and with an assignment like this:
public method(int variable = null):
The first makes a required nullable int argument named variable. The second won't even compile.
If you were to do
public method(int? variable = null)
Then you could call method as method(), not passing a value for variable and null will be inserted by the compiler call which is the default value provided in the parameter.
Related
This question already has answers here:
How to declare a constant Guid in C#?
(6 answers)
Why does C# limit the set of types that can be declared as const?
(6 answers)
Closed 3 years ago.
Out of curiosity, Is there a way to declare Guid.Empty, nay any Guid as const? I once thought all value types can be declared as a constant.
Line below returns the error: CS0283 The type 'Guid' cannot be declared const
const Guid myConstGuid = Guid.Empty;
The const modifier is only valid for primitive types.
You could however declare it as readonly.
This question already has answers here:
What is the purpose of a question mark after a value type (for example: int? myVariable)?
(9 answers)
Closed 7 years ago.
I have the following enigmatic declaration in C#:
Dictionary<string, DateTime?> badCameras = new Dictionary<string, DateTime?>();
This was written by a programmer no longer here & IS compiling! What ever does the '?' mean following DateTime object? Has it something to do with a struct? I've searched online & am finding nothing? BTW, this is .NET3.5.
Thanks
DateTime? is compiler sugar for Nullable<DateTime>. Effectively, nullable types allow for the use of null when dealing with value types that don't normally support null. See here for details.
The ? in that context is a shortcut for Nullable<T>. In your example, its equivalant to:
Dictionary<string, Nullable<DateTime>>
The main purpose is to allow value types to hold the null value.
This question already has answers here:
Is it possible to pass properties as "out" or "ref" parameters?
(3 answers)
Closed 8 years ago.
Why exactly I can't use an AutoProperty as an out parameter?
For example (This gives me an error):
public int HeightValue { get; set; }
//...
private void Parse()
{
int.TryParse(WidthText.Text, out HeightValue);
//Intellisense Error: out argument is not classified as a variable
}
Possibly because properties are in essence methods and you need to give a field to set the value to the out parameter. You can define a backing field for your property and give its value as the out parameter.
See Jon Skeet's answer here:
Passing a property as an 'out' parameter in C#
The method itself needs a variable as the out parameter. It's got to have a storage location it can just write values to. Not a property, not anything it needs to invoke: just a storage location. A property doesn't satisfy that requirement. So there's nothing that can be done by the compiler in the method to allow this.
This question already has answers here:
A curious C# syntax with a question mark
(3 answers)
Closed 9 years ago.
Could someone please tell me what the ? symbol means in the following code:
public Rectangle? Limits
{
get
{
return _limits;
}
set
{
It is syntactic sugar for specifying a nullable type.
It means it is a nullable type.
For example, DateTime? can be null, whereas DateTime cannot.
This question already has answers here:
Why is the default value of the string type null instead of an empty string?
(15 answers)
Closed 8 years ago.
Possible Duplicate:
The type ‘string’ must be a non-nullable type in order to use it as parameter T in the generic type or method ‘System.Nullable<T>’
As the title says, why are strings nullable by default in C#, but if I want, say, ints or doubles to be null, I have to explicitly say so?
Because string is a reference type, deriving from object and [most] other default types are value types, deriving implicitly from System.ValueType;