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;
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:
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.
This question already has answers here:
Why can the 'as' operator not be used to parse non-nullable value types?
(1 answer)
Unexpected behavior of "as" operator, it differs from plain cast
(3 answers)
Why can't I use the as keyword for a struct?
(6 answers)
Closed 3 years ago.
So I know the as operator in C# must be passed a reference or nullable type, because it can return null, what I don't understand is - why?
Returning null should be separate from what arguments are being passed to it, since I don't know its inner workings I can only draw on my knowledge of ordinary methods, which would be that what I return, normally, would have nothing to do with what arguments I pass.
In other words, I imagine the as operator to be something like this pseudocode:
private object as(object arg1, object arg2)
{
if (arg1 can be cast to arg2)
return (arg2)arg1;
else
return null;
}
Why would it matter if the passed argument is a reference or a value type, then?
Imagine you want to cast some object to int with help of as
int number = object as int;
If object is int the cast is successful and result is stored into int number. If the object is not int then conversion returns null and it cannot be stored in variable of non-nullable type. That's why compiler doesn't allow to use as with non-nullable types.
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:
Closed 10 years ago.
Possible Duplicate:
How do I enumerate an enum?
Say I have an enum type MyEnum. Is there a way in C# to get a list of all possible values for an enum of type MyEnum?
Enum.GetValues
An instance of the enum can have any assignable to the underlying type (i.e., int.MinValue through int.MaxValue for any regular enum). You can get a list of the named values by calling Enum.GetNames and Enum.GetValues.
Enum.GetValues(typeof(SomeEnum));
will return an array with all the values. I do not know if this helps you.