0

I am trying to develop a Windows 8 App in Visual Studio Express 2012 for Windows 8 using C#/XAML, and I am facing some problems while doing so.

I am from a Java background and have little knowledge of C#. I am trying to translate Java Enum to C#.

I stumbled upon Jon Skeet's Article which fits in my exact requirement. But from the comments and answers of this question, I realised that it's a proposed idea. My bad! :)

However, the closest translation I found after the above article is this one: https://stackoverflow.com/a/4778347/1127443

But even with the above implementation, VS cannot find the getCustomAttribute() method in Attribute.

How can I implement the idea of Java Enums in C# where common fields and abstract functions can be defined? Basically, I want to bring back the idea of enums as a set of allowed instances of a particular class with specific implementations.

Community
  • 1
  • 1
Rahul Thakur
  • 882
  • 16
  • 31
  • You read what you wanted to. This is a proposed syntax. – ChaosPandion Dec 01 '12 at 14:19
  • The syntax in that article is a *proposal*, not actual, working code. =) Even if it is a good one, and you pose a good question about how to actually do this. – J. Steen Dec 01 '12 at 14:19
  • from what I knwn enum in c# is only a list of value and nothing more – ESD Dec 01 '12 at 14:20
  • This has nothing to do with visual studio version or libraries. enums in C# are just some osrt of wrapper for int,long, byte ... You can´t add methods, or subclasses to it. The only possible way is probably using Extension Methods. But some sort of redesign would be probably better choice in your case. – Ondra Dec 01 '12 at 14:21
  • 1
    You should ask new question instead of changing this one. All answers are now deprecated. – Damian Leszczyński - Vash Dec 01 '12 at 14:46

3 Answers3

1

The article does not describe existing C# feature, but rather comes with a suggestion of what would be good to have implemented in future. It will not work.

Jarek Kardas
  • 8,445
  • 1
  • 31
  • 32
1

Here is what I would do to be as close to your code as possible:

Don´t use enum, but create a static class with some constant fields:

    public static class ArithmeticOperation
    {
        public static const Func<int, int, int> Addition = (x, y) => x+y;
        public static const Func<int, int, int> Subtraction = (x, y) => x - y;
        public static const Func<int, int, int> Multiplication = (x, y) => x * y;
        public static const Func<int, int, int> Division = (x, y) => x / y;
    }

You can use it just like any other method:

    static void Main(string[] args)
    {
        var result = ArithmeticOperation.Addition(1, 2);
    }

Or use it in a parameter just like an enum:

    private int DoCalculation(Func<int, int, int> operation)
    {
           return operation(1, 2);
    }

    ...

     DoCalculation(ArithmeticOperations.Addition);

You can pass any Func<int, int, int> as a parameter, not only ArithmeticOperation´s property.

Ondra
  • 1,619
  • 13
  • 27
0

You should read the article not only copy the source code

From article you have posted:

This post is a feature proposal for C# 4.0. (I suspect the lid for this kind of thing is closed on 3.0.)

The described functionality, was an attempt to make C# enhance enum like Java does. But for now such thing does not happen even in C# 4.0.

EDIT:

What type of benefit do you expect to gain using enum ? In Java such implementation is extreme rare.

You should understand the concept of enum, in general enums are used to have compile time constant readable by human. Instead of using number 0,1,2,3 we use in code aliases for those number ZERO, ONE, TWO, THREE. Typically enums are used to store some final structure like names of days, months etc.

And i think this is the reason why C# does not support enum like java.