Saturday, June 28, 2014

Passing untyped variables (C# equivalent of "template ")

In c++, I'm used to swap two variables --free from type-- as below.




...

template <typename VarType>
void swap (VarType *a, VarType *b) {
VarType &Tmp = &a;
&a = &b;
&b = &Tmp;
}
...


I'm sure there is a way to swap untyped vars in C# as well but couldn't figure out how. I've tried the code below; "GetType" part didn't work out. By the way, I'd prefer a safe solution (without the "/unsafe" switch), if exists.



unsafe public void Swap (TypedReference* a, TypedReference* b) {
Type T = a.GetType ();
}





No comments:

Post a Comment