C# keyword extern

I have enum types that are required in three C# files. One file inherits from one file where the inherited file contains the declaration. Therefore the remaining file also requires a reference to the enum types.

Would I use: “extern public enum enumName” or is there another method?

Thanks CSDG

with c#, most declarations / definitions made in one script are seen by the others.
but check this if you’re having trouble getting something to be recognized elsewhere and you’ve already made the actual definition public.

Extern is used when you bind with external dlls, not for referencing an enum within the same project.

Here’s an example of how you can access an enum defined inside another class’ scope:

class Foo
{
    public enum Bar
    {
        A, B, C
    }

    void Test()
    {
        Bar bar = Bar.A;
    }
}

class Example
{
    void Test()
    {
        Foo.Bar bar = Foo.Bar.B;
    }
}

Notice how you had to type out the class name before the enum name to access it from the other class.


Another version would be to put the enum outside the class all together:

enum Bar
{
    A, B, C
}

class Foo
{
    void Test()
    {
        Bar bar = Bar.A;
    }
}

class Example
{
    void Test()
    {
        Bar bar = Bar.B;
    }
}

Notice that we now can use the enum much more easily. All classes can use the enum without having to add any special code around it.

Thanks for the replies.

Still trying to learn the tags… NOT WORKING.

The problem, there were several classes: a FSMState class, a FSMSystem class, and a NPCControl class. Yet there were two files. The file FSMMachine.cs that contained the two former classes. In this file, I had to use the namespace FSMMachine.

In the other file, the NPCControl.cs contained the keyword “using” for the namespace that I had used. The FSMMachine.cs was not attached to anything, the NPCControl.cs was attached to my NPC GameObject.

The FSMState class was an abstract class, Unity would not allow me to attach it to a GameObject. The FSMSystem class was the Finite State machine. So these two classes were placed in the same file so other GameObjects could have access. This cut down any code redundancy. So all I had to worry about was the NPC control and its states.

The enumeration was also placed in the namespace FSMMachine. By using the namespace, everything was carried over into NPCControl including the enumeration.

Thanks again CSDG