Problem statement:
Typedef int to Tick and use it in three classes which are defined in three different files.
Files are: class1.cs, class2.cs and class3.cs
Solution 1:
Add this statement in all three files: class*.cs
using Tick = System.Int32
Disadvantage: If you choose to rename Tick to ClockTick, you need to change at three different places. You are violating the golden rule of one version of the truth.
Solution 2:
Derive out of the type you want to typedef’ed and use this instead.
Define a class Tick and derive it from Int32.
Disadvantage: This won’t work for non-user defined types like int, char, etc. It can work with ‘classes’.
For eg, this will work
public class TickList : List<int>
whereas this will not
public class Tick : System.Int32
Solution 3:
Create a wrapper around the type you want to be typedef’ed.
Disdvantage: It’s like using a saw to open a pack of biscuits. The solution is too much work compared to the problem. You need a typedef, and you should get a typedef and not a new class altogether!
public class Tick
{
int m_tick;
// Declare property here.
}
Conclusion:
C# does not support typedef, atleast not in the tradition C/C++ sense. Actually, this problem has an elegant solution in C/C++ (add this typedef in a .h file and include that file in all three source class*.c* files) because they have a concept of header files. C# removed header files and many useful features too.
Hi
I have the following typedef in a C++ project and I would like to migrate to c#
typedef int(*fooler)(int);
fooler pfool = (fooler) GetProcAddress(hm,”f00ler”);
How can I proceed?
Thanks
Laurent