Magos294963

Assume the following code:

Public class A<T>

{

public enum B

{

Alpha,

Beta,

}

}

A.B MyEnum1 = A.B.Alpha; //Won¡¯t work

A<int>.B MyEnum2 = A<int>.B.Alpha; //Works, but the enum shouldn¡¯t depend on the type, right

Why is it that, when accessing the enum, I have to specify the class's type The enum should, IMO, not depend on class A's type argument.



Re: Visual C# Language Enum in generic class

Mattias Sjogren

All nested types "inherit" the type parameters of their enclosing generic types. If you look at B with Ildasm, you'll see it as B<T> (even though it doesn't quite make sense to have a generic enum).






Re: Visual C# Language Enum in generic class

Magos

To extend the question, is there (like in Java) some static subclass concept in .NET In Java if you declare a subclass static it has no dependencies on its parent class (it's still instance-able, not like static outer classes).



Re: Visual C# Language Enum in generic class

Figo Fei - MSFT

Hi, Magos

According to your example, you don't need to define generics type, cause you didn't use it in the class.

If you defined it, you have to specify the type when using it, it's syntax which is quite nature, isn't it

And C# support static class and nested class. So you can define like this:

Class A

{

...

static Class B

{...}

}

In this way, class B can be regarded as a static member of the class A.

Thanks






Re: Visual C# Language Enum in generic class

Magos

Well obviously I use the type, this was just a simple example (just like the code below).

I don't think the syntax is "nature". If you use the enumeration outside the class it makes no sense specifying a type argument that is totally irrelevant. It is just confusing. Just to illustrate take this example of a boolean enumeration, why should you give the type "int" (or another type for that matter) all the time Total confusion!

Public class A<T>

{

public enum Bool

{

True,

False,

}

}

A<int>.Bool b = A<int>.False;

And about the static class, I'm not talking about the .NET static class (I tried it and it didn't work as I wanted to). I'm talking about how they work in Java. See this sample:

public class A<T>

{

public class B

{

}

public static class C

{

}

}

A<int> a = new A<int>();

A<int>.B b = a.new A<int>.B();

A.C c = new A.C();

If A was to be static it'd be the same as a .NET static class, however here the subclass C is static. Static subclasses are still instancible (can be created using new) but has no dependency to its parent class thus no need for a type argument.

This is a very important feature which .NET unfortunately lacks.

I also miss the feature as shown when createing the B class, the "a.new" meaning you create a class B that belongs to a specific class A.





Re: Visual C# Language Enum in generic class

Figo Fei - MSFT

Hi, Magos

In C#, "static" means some thing different from "dynamic", so you can't create a dynamic instance if the one modified by static.

Static class members can be used to separate data and behavior that is independent of any object identity: the data and functions do not change regardless of what happens to the object (that is the feature in .NET diff from java). Static classes can be used when there is no data or behavior in the class that depends on object identity.

And here is some tips of using static class in .NET: http://msdn2.microsoft.com/en-us/library/ms229038.aspx

According to the example you've showed I cannot see what is the potential problem. If you do have further problems, pls feel free to let us know. Thank you






Re: Visual C# Language Enum in generic class

Mattias Sjogren

Magos wrote:
To extend the question, is there (like in Java) some static subclass concept in .NET In Java if you declare a subclass static it has no dependencies on its parent class (it's still instance-able, not like static outer classes).

All nested types in C# are like static nested classes in Java, in that there's no automatic relation between an instance of the nested type and an instance of the enclosing type.