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.
Related
This question already has answers here:
Get the type name
(10 answers)
Closed 2 years ago.
I have an enumerable as follows:
IEnumerable<SomeObjectType> dataToImport;
At runtime I run the following code:
dataToImport.GetType().ToString()
So far so good. Checking results at runtime shows me something like the following :
System.Collections.Generic.List`1[SomeObjectType]
Can somebody tell me what that `1 means and where it is coming from? Should I expect this on all collections?
It's a placeholder for the first generic type argument. You should expect it on anything with a generic type.
The backtick(`) followed by a digit represent the number of generic arguments. For example List<T> has one generic argument hence `1
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:
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;
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there a way to omit out parameter?
Instead of:
SomeType param3;
SomeMethodCall(param1, param2, out param3);
I want not to define param3 in the case I don't need its value.
Is there any way to achieve this?
No, there is no way of doing that in C#.
If SomeMethodCall is something you defined yourself then you can overload the method. If it's not then you can't.
No, you'll have to actually create a variable of the correct type if you want to use a function that takes an out parameter.
This question already has answers here:
Is there a reasonable approach to "default" type parameters in C# Generics?
(6 answers)
Closed 9 years ago.
Is it possible to do something like
public class PriorityQueue<TValue, TPriority=int> where TPriority : IComparable
(note the =int) ?
Before you suggest it, yes, I know I can just add another line:
public class PriorityQueue<TValue> : PriorityQueue<TValue, int> { }
But I'm wondering if it's possible to do it as a param.
No. There is no option for default types on generic types in C#.
Your second example is often the "best" option available, if you need this behavior.