The code looks like this.
public class One<T> where T : Two {
public static int Y;
static One() {
// Y is to be initialised with the Z value from T
// this code does not compile
// Y = T.Z;
}
public int X { get { return Y; } }
}
public class Two {
public static int Z = 42;
}
public class Three {
public void Main() {
One<Two> a = new One<Two>();
Console.WriteLine("X = {0}", a.X); // should say 42
}
}
The idea is very simple: initialise the static value in a class from another static value in another class passed as a type parameter. There is nothing 'unsafe' about this, but the natural way to do it does not compile (see code).
I've searched SO and found a few near misses, but nothing that hits the spot. I've tried a few things including reflection, but to no avail. Any solution or even hints much appreciated.
Flydog's given you something you could use; I just wanted to make a quick note about my/jimi/enigmativity's comments:
It feel like you're hoping to subclass Two at some point and change out the Z; you reason that if your new class Three, which has a static Z of 43 and derives from Two, is used in combination with One then you'll end up with an .X that is 43. The problem comes in that static things cannot be overridden; they're resolved at compile time and they can look like overriding is at play in certain circumstances, but it's actually overloading or hiding and the compiler is picking something with a particular name in a defined order out of the inheritance tree
Because static things don't inherit (and arguably don't need to because they are hence always known to you, the developer, at compile time) there isn't the expectation that one day someone will subclass your code and write :
One<Four> a = new One<Four>();
And your code will need to pick up whatever they set Z to and use it. You know your Zs and can use them appropriately at compile tine and they know their Zs and should use them appropriately. But I understood where you were going with the T.Z thing..
It almost looks like it should work. Then you start typing T.Z and realize that your gut is agreeing with the compiler. T is a type parameter, it isn't a full fledged type-y thing.
Anyways, this works:
static One()
{
// Y is to be initialised with the Z value from T
var typeT = typeof(T);
var zFieldInfo = typeT.GetField("Z");
Y = (int) zFieldInfo.GetValue(null);
}
Yeah, it uses reflection, but it's only ever going to be run once.
If I have a method parameter that is an enum, intellisense will pick up the possible values for this enum and let me pick one. This isn't ideal for me however as it's possible people might want to use values outside of my defined set. If I make my argument a byte instead, I can then create a static class filled with consts that hold my defined set of values - the only downside is that intellisense does not know about this library of values. Is there a way to point intellisense towards a range of 'helper' values?
Technically you can assign 'invalid' values to your enum. Since the backing store of an enum is an int, you can assign any value to it:
public enum X
{
A = 0,
B = 1
}
class Program
{
static void Main(string[] args)
{
X x = (X)2;
}
}
That way, you can still have the IntelliSense support, and allow off-values. Of course, this has drawbacks too, so you have to consider whether they outweigh the pros.
A fix for that could be to assign 'custom' values in your enum, which you reserve for use later on:
public enum X
{
A = 0,
B = 1,
Custom1 = 2
}
To directly answer the Intellisense part of you question, then no I don't think it is possible to do that.
However I think you can solve your problem by using function overloading, this way you can use either type and have the benefits of both:
void Myfunction(MyEnum e)
{
MyFunction((byte)e);
}
void MyFunction(byte b)
{
// Do something
}
So I was going through some older code at work and came across this:
using Int16 = System.Int16;
using SqlCommand = System.Data.SqlClient.SqlCommand;
I have never seen a Namespace Declaration use an '=' before. What's the point of using it? Are there any benefits declaring things this way?
What else strikes me as odd is the fact that they even bothered declaring Int16. Doesn't visual studio know what an Int16 is just by typing it out?
The first line makes... erm... little sense, but it's not a namespace import; it is a type alias. For example, int is an alias for Int32. You are perfectly free to create your own aliases as you show in your example.
For example, let's say you have to imported namespaces with two types with the same name (System.Drawing.Point and System.Windows.Point come to mind...). You can create an alias to avoid fully qualifying the two types n your code.
using WinFormsPoint = System.Drawing.Point;
using WpfPoint = System.Windows.Point;
void ILikeMyPointsStructy( WinFormsPoint p ) { /* ... */ }
void IPreferReferenceTypesThankYou( WpfPoint p ) { /* ... */ }
The namespace alias is good for simplifying how you access certain types-- especially when you have many types with conflicting names.
For example, if you are referencing a couple of different namespaces where you've defined different sets of constants like:
namespace Library
{
public static class Constants
{
public const string FIRST = "first";
public const string SECOND = "second";
}
}
namespace Services
{
public static class Constants
{
public const string THIRD = "third";
public const string FOURTH = "fourth";
}
}
Then you decide to use both in a code file-- you will get a compilation error just by writing:
var foo = Constants.FIRST;
The alternative is to fully qualify your constants, which can be a pain, so the namespace alias simplifies it:
using Constants = Library.Constants;
using ServiceConstants = Service.Constants;
That being said, I don't know why you'd alias an Int16 as an Int16!
For those developers coming from a C++ background the construct can also be used as a sort of "local typedef" which helps simplify generic container definitions:-
using Index = Dictionary<string, MyType>;
private Index BuildIndex(. . .)
{
var index = new Index();
. . .
return index;
}
What is this Type in .NET? I am using reflection to get a list of all the classes and this one turns up.
What is it? where does it come from? How is the name DisplayClass1 chosen? I search the sources and didnt see anything. What does the <> mean? what does the c__ mean? is there reference?
It's almost certainly a class generated by the compiler due to a lambda expression or anonymous method. For example, consider this code:
using System;
class Test
{
static void Main()
{
int x = 10;
Func<int, int> foo = y => y + x;
Console.WriteLine(foo(x));
}
}
That gets compiled into:
using System;
class Test
{
static void Main()
{
ExtraClass extra = new ExtraClass();
extra.x = 10;
Func<int, int> foo = extra.DelegateMethod;
Console.WriteLine(foo(x));
}
private class ExtraClass
{
public int x;
public int DelegateMethod(int y)
{
return y + x;
}
}
}
... except using <>c_displayClass1 as the name instead of ExtraClass. This is an unspeakable name in that it isn't valid C# - which means the C# compiler knows for sure that it won't appear in your own code and clash with its choice.
The exact manner of compiling anonymous functions is implementation-specific, of course - as is the choice of name for the extra class.
The compiler also generates extra classes for iterator blocks and (in C# 5) async methods and delegates.
Jon is of course correct. I've provided a "decoder ring" for figuring out what the various compiler-generate type names mean here:
Where to learn about VS debugger 'magic names'
The names are quite long and we sometimes get complaints that we're bulking up the size of metadata as a result. We might change the name generation rules to address this concern at any time in the future. It is therefore very important that you not write code that takes advantage of knowledge of this compiler implementation detail.
I know this rather goes against the idea of enums, but is it possible to extend enums in C#/Java? I mean "extend" in both the sense of adding new values to an enum, but also in the OO sense of inheriting from an existing enum.
I assume it's not possible in Java, as it only got them fairly recently (Java 5?). C# seems more forgiving of people that want to do crazy things, though, so I thought it might be possible some way. Presumably it could be hacked up via reflection (not that you'd every actually use that method)?
I'm not necessarily interested in implementing any given method, it just provoked my curiosity when it occurred to me :-)
The reason you can't extend Enums is because it would lead to problems with polymorphism.
Say you have an enum MyEnum with values A, B, and C , and extend it with value D as MyExtEnum.
Suppose a method expects a myEnum value somewhere, for instance as a parameter. It should be legal to supply a MyExtEnum value, because it's a subtype, but now what are you going to do when it turns out the value is D?
To eliminate this problem, extending enums is illegal
You're going the wrong way: a subclass of an enum would have fewer entries.
In pseudocode, think:
enum Animal { Mosquito, Dog, Cat };
enum Mammal : Animal { Dog, Cat }; // (not valid C#)
Any method that can accept an Animal should be able to accept a Mammal, but not the other way around. Subclassing is for making something more specific, not more general. That's why "object" is the root of the class hierarchy. Likewise, if enums were inheritable, then a hypothetical root of the enum hierarchy would have every possible symbol.
But no, C#/Java don't allow sub-enums, AFAICT, though it would be really useful at times. It's probably because they chose to implement Enums as ints (like C) instead of interned symbols (like Lisp). (Above, what does (Animal)1 represent, and what does (Mammal)1 represent, and are they the same value?)
You could write your own enum-like class (with a different name) that provided this, though. With C# attributes it might even look kind of nice.
When built-in enums aren't enough, you can do it the old fashion way and craft your own. For example, if you wanted to add an additional property, for example, a description field, you could do it as follows:
public class Action {
public string Name {get; private set;}
public string Description {get; private set;}
private Action(string name, string description) {
Name = name;
Description = description;
}
public static Action DoIt = new Action("Do it", "This does things");
public static Action StopIt = new Action("Stop It", "This stops things");
}
You can then treat it like an enum like so:
public void ProcessAction(Action a) {
Console.WriteLine("Performing action: " + a.Name)
if (a == Action.DoIt) {
// ... and so on
}
}
The trick is to make sure that the constructor is private (or protected if you want to inherit), and that your instances are static.
Enums are supposed to represent the enumeration of all possible values, so extending rather does go against the idea.
However, what you can do in Java (and presumably C++0x) is have an interface instead of a enum class. Then put you standard values in an enum that implements the feature. Obviously you don't get to use java.util.EnumSet and the like. This is the approach taken in "more NIO features", which should be in JDK7.
public interface Result {
String name();
String toString();
}
public enum StandardResults implements Result {
TRUE, FALSE
}
public enum WTFResults implements Result {
FILE_NOT_FOUND
}
You can use .NET reflection to retrieve the labels and values from an existing enum at run-time (Enum.GetNames() and Enum.GetValues() are the two specific methods you would use) and then use code injection to create a new one with those elements plus some new ones. This seems somewhat analagous to "inheriting from an existing enum".
I didn't see anyone else mention this but the ordinal value of an enum is important. For example, with grails when you save an enum to the database it uses the ordinal value. If you could somehow extend an enum, what would be the ordinal values of your extensions? If you extended it in multiple places how could you preserve some kind of order to these ordinals? Chaos/instability in the ordinal values would be a bad thing which is probably another reason why the language designers have not touched this.
Another difficulty if you were the language designer, how can you preserve the functionality of the values() method which is supposed to return all of the enum values. What would you invoke this on and how would it gather up all of the values?
Adding enums is a fairly common thing to do if you go back to the source code and edit, any other way (inheritance or reflection, if either is possible) is likely to come back and hit you when you get an upgrade of the library and they have introduced the same enum name or the same enum value - I have seen plenty of lowlevel code where the integer number matches to the binary encoding, where you would run into problems
Ideally code referencing enums should be written as equals only (or switches), and try to be future proof by not expecting the enum set to be const
If you mean extends in the Base class sense, then in Java... no.
But you can extend an enum value to have properties and methods if that's what you mean.
For example, the following uses a Bracket enum:
class Person {
enum Bracket {
Low(0, 12000),
Middle(12000, 60000),
Upper(60000, 100000);
private final int low;
private final int high;
Brackets(int low, int high) {
this.low = low;
this.high = high;
}
public int getLow() {
return low;
}
public int getHigh() {
return high;
}
public boolean isWithin(int value) {
return value >= low && value <= high;
}
public String toString() {
return "Bracket " + low + " to " + high;
}
}
private Bracket bracket;
private String name;
public Person(String name, Bracket bracket) {
this.bracket = bracket;
this.name = name;
}
public String toString() {
return name + " in " + bracket;
}
}
Saw a post regarding this for Java a while back, check out http://www.javaspecialists.eu/archive/Issue161.html .
I would like to be able to add values to C# enumerations which are combinations of existing values. For example (this is what I want to do):
AnchorStyles is defined as
public enum AnchorStyles {
None = 0,
Top = 1,
Bottom = 2,
Left = 4,
Right = 8,
}
and I would like to add an AnchorStyles.BottomRight = Right + Bottom so instead of saying
my_ctrl.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
I can just say
my_ctrl.Anchor = AnchorStyles.BottomRight;
This doesn't cause any of the problems that have been mentioned above, so it would be nice if it was possible.
A temporary/local workaround, when you just want very local/one time usage:
enum Animals { Dog, Cat }
enum AnimalsExt { Dog = Animals.Dog, Cat= Animals.Cat, MyOther}
// BUT CAST THEM when using:
var xyz = AnimalsExt.Cat;
MethodThatNeedsAnimal( (Animals)xyz );
See all answers at: Enum "Inheritance"
You can't inherit from/extend an enum, you can use attributes to declare a description. If you're looking for an integer value, that's built-in.
Hmmm - as far as I know, this can't be done - enumerations are written at design-time and are used as a convenience to the programmer.
I'm pretty sure that when the code is compiled, the equivalent values will be substituted for the names in your enumeration, thereby removing the concept of an enumeration and (therefore) the ability to extend it.
Some time back even i wanted to do something like this and found that enum extensions would voilate lot of basic concepts... (Not just polymorphisim)
But still u might need to do if the enum is declared in external library and
Remember you should make a special caution when using this enum extensions...
public enum MyEnum { A = 1, B = 2, C = 4 }
public const MyEnum D = (MyEnum)(8);
public const MyEnum E = (MyEnum)(16);
func1{
MyEnum EnumValue = D;
switch (EnumValue){
case D: break;
case E: break;
case MyEnum.A: break;
case MyEnum.B: break;
}
}
As far as java is concerned it is not allowed because adding elements to an enum would effectively create a super class rather than a sub class.
Consider:
enum Person (JOHN SAM}
enum Student extends Person {HARVEY ROSS}
A general use case of Polymorphism would be
Person person = Student.ROSS; //not legal
which is clearly wrong.