Can I define C# aliases/keywords for my own classes like int, string, object?

Viewed 2374

Its not about using foo = MyPackage.Foo;

I realize this has more to do with the IDE (Visual Studio 2010) that shows me special types in blue. And I want the same for some of my classes. I want it blue (presented as special) and be accessible in the whole project.

The reason is that I want to give them meaning/importance so that everyone in my team knows this class is a key class of the whole project.

// "foo" and "Foo" shall be just the same type:
foo:     MyPackage.Foo

object:  System.Object
string:  System.String
bool:    System.Boolean
byte:    System.Byte
sbyte:   System.SByte
short:   System.Int16
ushort:  System.UInt16
int:     System.Int32
uint:    System.UInt32
long:    System.Int64
ulong:   System.UInt64
float:   System.Single
double:  System.Double
decimal: System.Decimal
char:    System.Char

Is that possible ? If there would be a file in the installation of my IDE and there would be the above keywords inside and I could add some keywords to it and save it and restart the IDE ?

To have 2 classes do the very same I could do this. It would be 2 names for the same class. I know I would need to cast between those.

public class foo : Foo
{ }
5 Answers

If you want to define a keyword (like int or void) for such a class, it is not supported in C#. Keywords are predefined, reserved identifiers, and only a change to the compiler would make it possible to do what you intent.

The closest approach would be to specify a class/namespace alias, as shown by @Soner Gönül. However, since this applies to a single file, you would have to pre-include it in every project file, as expressed in this post.

Now, the keywords are colored by the IDE based on the fact that they are keywords (in the sense that they are listed somewhere in the IDE's syntax highlighting engine as keywords for the C# language). Depending on the IDE you are using, you might be able to add an identifier for that class to look like a keyword. However, you would still need to import an alias for it in each file, otherwise the compiler won't recognize it.

Your question seems to be about two distinct requirements:

  1. Define a "global" symbol/class, available everywhere.
  2. Apply a distinct color (i.e. syntax highlighting) for that symbol.

1) Not directly possible. What you could do in this case is providing an assembly containing that class, and enforce its usage by your team. You could even use a static analysis tool and trigger some compile errors (say, if that assembly is not referenced).

2) This is even harder to implement. Customizing the default syntax colorization mechanism for C# in Visual Studio is possible, but definitely not easy.

Visual studio supports a given language (with syntax highlighting, IntelliSense and so forth) through a Language Service. So it could be feasible to add a custom set of keywords, with a distinct color palette, by following these steps. You would also have to find out how to to plug your custom code into the already defined C# Language Service.

PS: Never tried doing that, and I get tired just looking at the work that seems to be needed. But I guess that's what the JetBrains ReSharper team did.

PPS: If you were using Managed C++ instead of C# it would be much simpler.

Are you looking using directive like this?

using foo = MyPackage.MyClass;

EDIT: using directive is for only namespaces or types. What you looking sounds not possible to me but I keep searching..

The only way to really accomplish this is to create an assembly to reference into your project (or just create a class in your project if you don't want an external assembly).

In your assembly, you could remove the namespace and have it with just a class(es) like this:

public class mytype    
{
    public void DoSomething()
    {

    }
}
public class MyType : mytype
{

}

If you wanted both upper and lower case classes, just inherit the base class.

Then in your application, once you add it as a reference, you can just access it:

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            mytype my_type = new mytype();
            my_type.DoSomething();

            MyType my_Type = new MyType();
            my_Type.DoSomething();

        }
    }
}

Granted, the color will be the standard class color, but would allow you to at least access your custom "type" that you have defined throughout your project.

Hope this leads you in the right direction.

You can't do what you're attempting to do; for example, string is an alias to System.String within the C# language; it isn't possible to add to that list of aliases, as that would involve modifying the language.

Related