3

I have the following example code. Why do I have to specify a type for T? Is this something that I can take advantage of? I do not see why the enum definition is depending on the type argument? How can the contents of the enum be something else depending on the type? So in my syntactic sugar favor I would like to leave the definition of the type out.

public class SomeGenericClass<T>
{
    public enum InitialisationMode
    {
        UseDefaultValues,
        DoOtherThings
    }

    public SomeGenericClass(InitialisationMode initMode = InitialisationMode.UseDefaultValues)
    {

    }
}

public class SomeOtherClass
{
    public void DoAction()
    {
        //Why do I have to specify the generic type arguments when using the enum value?
        var genericClass = new SomeGenericClass<int>(SomeGenericClass<int>.InitialisationMode.DoOtherThings);
        //I Would expect to be able to use the enum like the line below:
        genericClass = new SomeGenericClass<int>(SomeGenericClass.InitialisationMode.DoOtherThings);
    }
}

I know I could workd around this by doing something like this, or put the enum one scope up:

public class SomeGenericClass<T>
{
    public SomeGenericClass(SomeGenericClass.InitialisationMode initMode = SomeGenericClass.InitialisationMode.UseDefaultValues)
    {

    }
}

public abstract class SomeGenericClass
{
    public enum InitialisationMode
    {
        UseDefaultValues,
        DoOtherThings
    }
}

public class SomeOtherClass
{
    public void DoAction()
    {
        //I Would expect to be able to use the enum like the line below:
        var genericClass = new SomeGenericClass<int>(SomeGenericClass.InitialisationMode.DoOtherThings);
    }
}
4
  • 5
    The T is not being used anywhere. What's the reason to declare generic parameter if it's not being used? Commented Apr 14, 2015 at 15:09
  • 2
    Curious as to the reason to declare the enum in the class when you need to use it publicly. Why not declare it outside of the class and avoid the issue? Commented Apr 14, 2015 at 15:12
  • Hmya, them are the rulez. Why not just punt the problem and move the enum type outside of the class? Commented Apr 14, 2015 at 15:17
  • @haim770 As this code its soul purpose is to show how to refer to that enum, all other code clouds the exemplary purpose of the code. Commented Apr 14, 2015 at 15:35

1 Answer 1

4

Because your enum lives in that class, and it would be possible (if not very practical) to have that enumeration be different for different types:

class Program
{
    static void Main(string[] args)
    {
        var sgc = new SomeGenericClass<string>("asdf");

        var sgc2 = new SomeGenericClass<int>(1);

        var sgc3 = new SomeNonGenericChild("asdf2");
        Console.ReadKey();
    }
}

public class SomeGenericClass<T>
{
    public enum InitialisationMode
    {
        UseDefaultValues,
        DoOtherThings = 3
    }


    public SomeGenericClass(T blah, InitialisationMode initMode = InitialisationMode.UseDefaultValues)
    {

        Console.WriteLine(blah.GetType().Name + "; " + initMode);
    }
}

public class SomeNonGenericChild : SomeGenericClass<string>
{
    public new enum InitialisationMode
    {
        UseDefaultValues,
        DoEvenMoreThings
    }

    public SomeNonGenericChild(string blah, InitialisationMode initMode= InitialisationMode.DoEvenMoreThings) : base(blah)
    {

        Console.WriteLine(blah.GetType().Name + "; " + initMode);
    }
}

To achieve the syntax you want, you could do something like:

namespace SomeNamespace
{
    public enum InitialisationMode
    {
        UseDefaultValues,
        DoOtherThings
    }
    public class SomeGenericClass<T>
    {

        public SomeGenericClass(InitialisationMode initMode = InitialisationMode.UseDefaultValues)
        {

        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes thank you for your example, I see. Could you elaborate (by example maybe?) on what you said:and it would be possible (if not very practical) to have that enumeration be different for different types..
The example I just posted is about as close as I can get. There's probably a better/more sophisticated way of doing this, but the point is the enumeration definition can be changed for different instances here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.