Default for generic type? [duplicate] - c#

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.

Related

Single quotation with a number when getting the type of IEnumerable [duplicate]

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

Is there a way to shorten field declaration in C#? [duplicate]

This question already has answers here:
Is there a way to abbreviate a custom class type declaration?
(4 answers)
Implicit typing; why just local variables?
(6 answers)
Closed 2 years ago.
Why do I have to write:
public VeryLongClassNameThatHurtsMyEyes<AnotherVeryLongClassName> field = new VeryLongClassNameThatHurtsMyEyes<AnotherVeryLongClassName>();
Instead of:
public var field = new VeryLongClassNameThatHurtsMyEyes<AnotherVeryLongClassName>();
Is there any way to shorten this ridiculous declaration?
Why do I have to write two identical types in a single line?
Upd.
I have found that using "dynamic" keyword instead of "var" perfectly solves this problem!
Please feel free to provide any info on the perfomance (or other) issues with this solution!
You can use a type alias. Documentation can be found here. Link to alias for generic class: here
using YourShortName = VeryLongClassNameThatHurtsMyEyes<AnotherVeryLongClassName>;
Usage
public YourShortName field = new YourShortName();
See here for why you can only use var for local variables.
And see here for an in depth look at dynamic and why it's not even remotely close to using var.

About Generic types with like T [duplicate]

This question already has answers here:
What does "where T : class, new()" mean?
(11 answers)
Closed 9 years ago.
I wonder what is this? This kind of a generic method I think. It has a part with 'where'. What about that? There is also generic classes I've heard. How can I learn these can you recommend an article?
protected T Item<T>() where T : class
{
return GetDataItem() as T ?? default(T);
}
The where clause is called a "generic constraint". In that case, where T: class dictates that T must be a reference type (i.e., not a struct).
More info on generic constraints: http://msdn.microsoft.com/en-us/library/d5x73970.aspx
And generic classes: http://msdn.microsoft.com/en-us/library/sz6zd40f.aspx
Edit
In the snippet you provided, the constraint is needed because otherwise the null-coalescing operator (??) wouldn't make sense, since value types (structs) can't be null.

Is a generic type MyType<,> actually a type? [duplicate]

This question already has answers here:
If A<T1,T2> is a template for actual type, then why is typeof(A<,>) allowed?
(3 answers)
Closed 9 years ago.
I have some confusion with this that arose from messing around with exporting generic types in MEF
I noticed:
new Dictionary<string,bool>().GetType() == typeof(Dictionary<,>)
false
new Dictionary<string,bool>().GetType().GetGenericTypeDefinition() == typeof(Dictionary<,>)
true
Yet Dictionary<,> itself is not considered a ‘type’ as this will actually generate a compile error:
new Dictionary<string,bool> as Dictionary<,>
Type expected
new Dictionary<string,bool> is Dictionary<,>
Type expected
So my question is, is Dictionary<,> actually a type? Does .NET treat generic types differently than non-generic types?
Now in MEF I can export a generic class as
[Export(typeof(MyGenericClass<,>))]
And this would satisfy an import requirement like
[Import]
public MyGenericClass<string, long> Instance { get; set; }
I'm confused about the type-system's rules here
See What exactly is an “open generic type”. What you are referring to is called an unbound generic type and is explained in the same post. An unbound generic type is actually a type, however it can only be used within a typeof() expression. Note: Unlike C# Java allows expressions like List<?>.
Yes, MyType<,> is a type.
It's a "open generic" Type, See What exactly is an "open generic type" in .NET?

Is it possible to have a generic Addition method [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicates:
How to cast an object into its type?
Solution for overloaded operator constraint in .NET generics
Hi All,
Is it possible to have a generic Addition method?
something like:
public void Add<T>(T a, T b)
{
T c = a + b;//Error
}
Actually operands '+' can not be applied to Type T. Is there any way around?

Categories

Resources