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.
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.
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.
They are largely the same, except that: The alias declaration is compatible with templates, whereas the C style typedef is not.
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.
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.
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++.
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.
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.
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;
:)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With