Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of typedef in C#

Tags:

c#

typedef

Is there a typedef equivalent in C#, or someway to get some sort of similar behaviour? I've done some googling, but everywhere I look seems to be negative. Currently I have a situation similar to the following:

class GenericClass<T>  {     public event EventHandler<EventData> MyEvent;     public class EventData : EventArgs { /* snip */ }     // ... snip } 

Now, it doesn't take a rocket scientist to figure out that this can very quickly lead to a lot of typing (apologies for the horrible pun) when trying to implement a handler for that event. It'd end up being something like this:

GenericClass<int> gcInt = new GenericClass<int>; gcInt.MyEvent += new EventHandler<GenericClass<int>.EventData>(gcInt_MyEvent); // ...  private void gcInt_MyEvent(object sender, GenericClass<int>.EventData e) {     throw new NotImplementedException(); } 

Except, in my case, I was already using a complex type, not just an int. It'd be nice if it were possible to simplify this a little...

Edit: ie. perhaps typedefing the EventHandler instead of needing to redefine it to get similar behaviour.

like image 214
Matthew Scharley Avatar asked Oct 02 '08 09:10

Matthew Scharley


People also ask

What is the equivalent of typedef in C#?

the typedef Keyword Equivalent in C# The typedef keyword is not available in C#. Unfortunately, there isn't any keyword equivalent to the typedef keyword of the C and C++ programming languages present in the C#. The only real solution to this problem is to keep the user-defined data-type names short and meaningful.

Is there typedef in C?

typedef is a reserved keyword in the programming languages C and C++. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type.

Is typedef the same as using?

They are largely the same, except that: The alias declaration is compatible with templates, whereas the C style typedef is not.

What is typedef and #define in C?

typedef is limited to giving symbolic names to types only, whereas #define can be used to define an alias for values as well, e.g., you can define 1 as ONE, 3.14 as PI, etc. typedef interpretation is performed by the compiler where #define statements are performed by preprocessor.

What is a typedef in C language?

The C programming language supports various keywords and data types. In C, you can also create your own data type. Typedef is a predefined keyword. This keyword helps in creating a user defined name for an existing data type. What is the typedef keyword in C? As discussed, it is a predefined keyword.

What is the meaning of the typedef?

The typedef keyword is a reserved keyword in C and C++ programming languages. The typedef keyword assigns a new name to a pre-existing data-type. The following code example shows how we can rename a data-type using the typedef keyword in C++.

What is the difference between class and typedef in Java?

In Java, class is used to name and construct types or we can say that class is the combined function of C++’s struct and typedef. But that is totally different thing and not the equivalent of typedef anywhere. typedef: It is a keyword not a function that is used in C/C++ language to assign alternative names to existing data types.

How to use typetypedef with pointers in C/C++?

typedef will also work with the pointer in C/C++ language like renaming existing keywords. In case of pointers * binds in the right not the left side.


2 Answers

No, there's no true equivalent of typedef. You can use 'using' directives within one file, e.g.

using CustomerList = System.Collections.Generic.List<Customer>; 

but that will only impact that source file. In C and C++, my experience is that typedef is usually used within .h files which are included widely - so a single typedef can be used over a whole project. That ability does not exist in C#, because there's no #include functionality in C# that would allow you to include the using directives from one file in another.

Fortunately, the example you give does have a fix - implicit method group conversion. You can change your event subscription line to just:

gcInt.MyEvent += gcInt_MyEvent; 

:)

like image 145
Jon Skeet Avatar answered Nov 16 '22 01:11

Jon Skeet


Jon really gave a nice solution, I didn't know you could do that!

At times what I resorted to was inheriting from the class and creating its constructors. E.g.

public class FooList : List<Foo> { ... } 

Not the best solution (unless your assembly gets used by other people), but it works.

like image 24
Jonathan C Dickinson Avatar answered Nov 16 '22 01:11

Jonathan C Dickinson