Default parameter for a generic class.

Hello,

Is there a way I can give TType a default value, for example:

public interface MyInterface<TType=MyDefinedType>{
	....
}

Open in new window


Thanks,
Uni
LVL 3
Unimatrix_001Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DhaestCommented:
I'm not sure if this can help you ...

Constructor Constraint
Suppose you want to instantiate a new generic object inside a generic class. The problem is the C# compiler does not know whether the type argument the client will use has a matching constructor, and it will refuse to compile the instantiation line.

To address this problem, C# allows you to constrain a generic type parameter such that it must support a public default constructor. This is done using the new() constraint. For example, here is a different way of implementing the default constructor of the generic Node <K,T> from Code block 3.

class Node<K,T> where T : new()
{
   public K Key;
   public T Item;
   public Node<K,T> NextNode;
   public Node()
   {
      Key      = default(K);
      Item     = new T();
      NextNode = null;
   }
}
You can combine the constructor constraint with derivation constraint, provided the constructor constraint appears last in the constraint list:

public class LinkedList<K,T> where K : IComparable<K>,new()
{...}

Source: An Introduction to C# Generics
http://msdn.microsoft.com/en-us/library/ms379564(v=vs.80).aspx


default Keyword in Generic Code (C# Programming Guide)
http://msdn.microsoft.com/en-us/library/xwth0h0d.aspx
0
Unimatrix_001Author Commented:
Hi Dhaest,

Thanks for the comment - unfortunately I don't think that's quite what I'm after. I'm simply wanting to make it so when I use an interface I don't have to specify TType and let it default to something.

Thanks,
Uni
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Unimatrix_001Author Commented:
Thank you. :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.