Int32 type alias problem!
-
using Integer = System.Int32;
...Integer i = 0;
Keep in mind that the C#
int
keyword is an alias for System.Int32.Tech, life, family, faith: Give me a visit. I'm currently blogging about: Check out this cutie The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
wow, thanks a lot!
-
using Integer = System.Int32;
...Integer i = 0;
Keep in mind that the C#
int
keyword is an alias for System.Int32.Tech, life, family, faith: Give me a visit. I'm currently blogging about: Check out this cutie The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
hey Judah, thanks from me too! i thought aliasing we can do only for namespaces!!
-
hey Judah, thanks from me too! i thought aliasing we can do only for namespaces!!
No, you can alias types, too. There is one caveat: unlike C/C++, aliases are per-file. Each file that wants to use the Integer alias described above would have to have to specify it in using declarations. They are especially useful with generics, IMO.
using IntStringPair = System.Collections.Generic.KeyValuePair<int, string>;
...
List<IntStringPair> listOfPairs = ...;Tech, life, family, faith: Give me a visit. I'm currently blogging about: Check out this cutie The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
No, you can alias types, too. There is one caveat: unlike C/C++, aliases are per-file. Each file that wants to use the Integer alias described above would have to have to specify it in using declarations. They are especially useful with generics, IMO.
using IntStringPair = System.Collections.Generic.KeyValuePair<int, string>;
...
List<IntStringPair> listOfPairs = ...;Tech, life, family, faith: Give me a visit. I'm currently blogging about: Check out this cutie The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
Judah Himango wrote:
using IntStringPair = System.Collections.Generic.KeyValuePair;
That's a shame, I hated when people did that in C++....
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Judah Himango wrote:
using IntStringPair = System.Collections.Generic.KeyValuePair;
That's a shame, I hated when people did that in C++....
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Heh, well, at least it's limited in scope. It's true, I always hate having to track down aliases in C++. Or worse, aliases of aliases of aliases...etc. I don't think you can do alias an alias in C#, which is good. Question is, is the aliasing worth it? Which one would you rather encounter:
List<KeyValuePair<int, string>> listOfPairs = new List<KeyValuePair<int, string>>();
foreach(KeyValuePair<int, string> pair in listOfPairs)
{
...
}or
using IntStringPair = System.Collections.Generic.KeyValuePair<int, string>;
...
List<IntStringPair> listOfPairs = new List<IntStringPair>();
foreach(IntStringPair pair in listOfPairs)
{
...
}Tech, life, family, faith: Give me a visit. I'm currently blogging about: Check out this cutie The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Heh, well, at least it's limited in scope. It's true, I always hate having to track down aliases in C++. Or worse, aliases of aliases of aliases...etc. I don't think you can do alias an alias in C#, which is good. Question is, is the aliasing worth it? Which one would you rather encounter:
List<KeyValuePair<int, string>> listOfPairs = new List<KeyValuePair<int, string>>();
foreach(KeyValuePair<int, string> pair in listOfPairs)
{
...
}or
using IntStringPair = System.Collections.Generic.KeyValuePair<int, string>;
...
List<IntStringPair> listOfPairs = new List<IntStringPair>();
foreach(IntStringPair pair in listOfPairs)
{
...
}Tech, life, family, faith: Give me a visit. I'm currently blogging about: Check out this cutie The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
I'd prefer the former, because there's no question of what it is. Actually, I would create a struct that names the int and string according to what they represent.
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
-
Judah Himango wrote:
using IntStringPair = System.Collections.Generic.KeyValuePair;
That's a shame, I hated when people did that in C++....
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Christian Graus wrote:
That's a shame, I hated when people did that in C++....
I think they are useful, since you haven't to interpretate again and again complex expressions. Anyway it's a matter of taste.:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
Judah Himango wrote:
using IntStringPair = System.Collections.Generic.KeyValuePair;
That's a shame, I hated when people did that in C++....
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Christian Graus wrote:
That's a shame, I hated when people did that in C++....
That depends how! With a define or a typedef?
**
xacc.ide-0.2.0.57 - now with C# 2.0 parser and seamless VS2005 solution support!
**
-
I'd prefer the former, because there's no question of what it is. Actually, I would create a struct that names the int and string according to what they represent.
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
Interesting. I'd prefer the latter because it is descriptive enough by its name alone and it's easier on the eyes than the double generic type. But, I do see where you're coming from. Aliases are certainly open to abuse, albeit on a much smaller level than C++ #defines. Come to think of it, I can't say I've seen alias abuse in any project I've worked on, especially nothing so terrible as abominations like A Better C[^] or VB++[^]. :)
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Check out this cutie The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Interesting. I'd prefer the latter because it is descriptive enough by its name alone and it's easier on the eyes than the double generic type. But, I do see where you're coming from. Aliases are certainly open to abuse, albeit on a much smaller level than C++ #defines. Come to think of it, I can't say I've seen alias abuse in any project I've worked on, especially nothing so terrible as abominations like A Better C[^] or VB++[^]. :)
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Check out this cutie The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
I've worked on C++ projects where I was always going back to see what the typedefs meant. I hated it.
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
-
I've worked on C++ projects where I was always going back to see what the typedefs meant. I hated it.
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
I agree with Chris. I've dealt with C++ projects where the developer went crazy with typedefs. Even though C# has a similar ability, it is much simpler to use the native data type names. They may not be as short as we want or as clear within the specific application domain, but they are unambigous and standard. I don't have to worry about a new developer trying to understand what an IntStringPair is, or look for it on MSDN.
KeyValuePair
clearly defines the object using terms that everyone is familar with.----------------------------- In just two days, tomorrow will be yesterday.