Link to home
Start Free TrialLog in
Avatar of David DB
David DB

asked on

Restictions on T

Hi

Is the only way to have restrictions on T to make an interface, or are there any other way ?

Since "where T:" can have only one class:

public object Test<T>(int whatType) where T:testclass

Open in new window

Avatar of it_saige
it_saige
Flag of United States of America image

The where clause is a constraint clause.  So each thing that you add to your where clause further constrains the list of available types that can call the method.

Consider the following -
static class Extensions
{
    public static string GetName<T>(this T source) where T : Person, ISomeNamer, new()
    {
        return source.Name;
    }
}

Open in new window

Implementations -
interface ISomeNamer
{
    string Name { get; set; }
}

public class Person : ISomeNamer
{
    public string Name { get; set; }
}

public class Employee : ISomeNamer
{
    public string Name { get; set; }
}

public class Dog
{
    public string Name { get; set; }
}

Open in new window

If I call the GetName method with a new Person instance I will get a name, but I will not be able to call the GetName method using new instances of an Employee or Dog because it is constrained to only classes which are of type Person, implement the ISomeNamer interface and have a default parameterless constructor.

Proof of concept -
using System;

namespace EE_Q29173426
{
    class Program
    {
        static void Main(string[] args)
        {
            var person = new Person { Name = "Mary" };
            var employee = new Employee { Name = "Paul" };
            var dog = new Dog { Name = "Rusty" };

            Console.WriteLine(person.GetName());
            Console.WriteLine(employee.GetName());
            Console.WriteLine(dog.GetName());

            Console.ReadLine();
        }
    }

    interface ISomeNamer
    {
        string Name { get; set; }
    }

    public class Person : ISomeNamer
    {
        public string Name { get; set; }
    }

    public class Employee : ISomeNamer
    {
        public string Name { get; set; }
    }

    public class Dog
    {
        public string Name { get; set; }
    }

    static class Extensions
    {
        public static string GetName<T>(this T source) where T : Person, ISomeNamer, new()
        {
            return source.Name;
        }
    }
}

Open in new window

Give the following errors:User generated imageYou would think to fix this that you would simply add the Employee type to the where clause, but you cannot do that, instead what you would have to do is make the Employee class a child of the Person class; e.g -
public class Employee : Person
{
}

Open in new window

Now the instance of the Employee class can call the GetName method.

-saige-
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial