Bruno Brant
Bruno Brant

Reputation: 8564

Anyway to default a generic parameter to a certain type?

Is there a way to provide a default type for a parameter T of a generic, something like:

class Something<T = string>
{
}

I know there aren't many strong reasons for this, but I would like to hint the code client which type he should be preferably using.

Another thing, can I restrict the generic type to a ValueType? I've just seen that you can't, but still, I'd like to know why. Anyone has a clue?

Thanks!

Upvotes: 22

Views: 15493

Answers (5)

AiApaec
AiApaec

Reputation: 660

You need a subclass. Recently I needed something like that, this is my solution:

    public abstract class MyGenericClass<T1, T2>
    {
        public abstract void Do(T1 param1, T2 param2);
    }

    public class Concrete : MyGenericClass<string, int?>
    {        
        public override void Do(string param1, int? param2 = null)
        {
            Console.WriteLine("param1: {0}", param1);

            if (param2 == null)
                Console.WriteLine("param2 == null");
            else
                Console.WriteLine("param2 = {0}", param2);

            Console.WriteLine("=============================================");
        }        
    }

You can call the method:

    string param1 = "Hello";
    Concrete c = new Concrete();
    c.Do(param1);
    c.Do(param1, 123);
    c.Do(param1, (int?)null);
    /*  Result:
    param1: Hello
    param2 == null
    =============================================
    param1: Hello
    param2 = 123
    =============================================
    param1: Hello
    param2 == null
    =============================================
    */

I prefer to use null default values since i read this: C# In Depth – Optional Parameters and Named Arguments

Upvotes: 0

Vercas
Vercas

Reputation: 9141

Ok, I suppose you have the class:

class Something<T>
{

}

Now, you might want another class:

class Something : Something<string>
{
    // NO MORE CODE NEEDED HERE!
}

This is the only and the best way.
So if one uses Something he will actually use Something<string>.

Upvotes: 34

Patko
Patko

Reputation: 4423

According to the docs you can constrain T to be a value type using the where keyword

where T: struct

Class declaration would then look like this:

class Something<T> where T: struct {
  ...
} 

Though string is not a value type and you won't be able to use it with such a constraint. Maybe IComparable would be better in case you want to also use the string type.

As for specifying the default type, I don't believe it is possible.

Upvotes: 3

Donut
Donut

Reputation: 112815

You can use the where keyword to constrain the specific types that can be used as type parameters.

For example, you could your class to only accept generic type parameters where the type implements the IComparable interface:

class Something<T> where T : IComparable
{
}

Or, as you mentioned, you can constrain T to be a value type by using where T : struct:

class Something<T> where T : struct
{
}

See MSDN's article on Constraints on Type Parameters for more info. Hope this helps!

Upvotes: 2

bradenb
bradenb

Reputation: 793

I don't believe there is a way to default it to a certain type, but you could put that as a comment in the XML docs. As far as restricting to a value type, you can obtain this behavior by declaring your generic as follows:

class MyGeneric<T> where T : struct
{
    ...
}

Upvotes: 2

Related Questions