Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify a C# Generic Constraint to be of ClassA OR ClassB?

Tags:

c#

generics

Is there a way to specify that a generic type be of one type or another type?

public class SoftDrink<T>
    where T : TypeOne or TypeTwo
{ }
like image 681
Doctor Blue Avatar asked Jan 15 '10 19:01

Doctor Blue


People also ask

What does AC mean?

a/c (plural a/cs) Abbreviation of account. a cheque marked a/c payee may not be deposited in a bank account which does not belong to the named payee. Initialism of air conditioning.

What does AC mean in business?

A/C is an abbreviation for account/ current An account/ current is used to help determine a company's balance of trade.

What does AC in medical terms mean?

a.c.: Abbreviation on a prescription meaning before meals; from the Latin "ante cibum", before meals. This is one of a number of abbreviations of Latin terms that have traditionally been used in writing prescriptions.

What is AC account name?

AC Account means an annual compensation account maintained under the Plan for a Participant in accordance with Article III.


1 Answers

Nope because that wouldn't make any sense. If you try to access a member function of "T", how can the compiler tell which base class to try and resolve members of?

You could try creating a common interface for TypeOne and TypeTwo and having the constraint specify that interface? What are you trying to accomplish, exactly?

like image 79
Sapph Avatar answered Sep 19 '22 12:09

Sapph