isnt there anything like typedef in C# like c++

  • Thread starter Thread starter Abubakar
  • Start date Start date
A

Abubakar

Hi,
I am writing C# code in vs2k5, and I write statements like:
List<KeyValuePair<string, string>> dlist;
the specifying of the type above is looking uglier as I use it in many
places in the code, and its also a lot of typing also. Is there anything
that can reduce it to some single short name and I can use that instead,
just like we can do in c++?

regards,

...ab
http://joehacker.blogspot.com
 
Abubakar said:
I am writing C# code in vs2k5, and I write statements like:
List<KeyValuePair<string, string>> dlist;

class Foo : List<KeyValuePair<string, string>> { }
Foo dlist;

I am curious if there's a better way, though.
 
Hi,
I am writing C# code in vs2k5, and I write statements like:
List<KeyValuePair<string, string>> dlist;
the specifying of the type above is looking uglier as I use it in many
places in the code, and its also a lot of typing also. Is there anything
that can reduce it to some single short name and I can use that instead,
just like we can do in c++?

Sort of. You can use the "using" directive to define an alias to a type.
For example, at the very top of your file or namespace declaration:

using ShortDict = System.Collections.Generic.List<KeyValuePair<string,
string>>;

http://msdn.microsoft.com/en-us/library/sf0df423.aspx

Note that lacking a pre-processor, you can't create a single include file
that defines this kind of mapping for multiple source files. You'd have
to specify the alias in each file where you want to use it.

Pete
 
Abubakar said:
Thanks, its better than nothing. Should propose this to be a feature
in C#.

Given the way C# and .NET works and how the preferred programming
style is, then it would not make sense to add it.

You should not code C++ style in C# (or C# style in C++).

Arne
 
You should not code C++ style in C# (or C# style in C++).

No I'm not at all coding in C++ style here, its just a matter of having to
uselessly write so much! Some languages have just made the right decisions
in some things, if we are already following (in the things that we r
following), we should follow the nice logical things/features.
While I think I know where u are coming from when you say "Given the way C#
and .NET works ..", but I don’t think that this is one of those cases, imo.

....
List<KeyValuePair<string, string>> dlist = (List<KeyValuePair<string,
string>>)Session["dislist"];
dddis.Items.Clear();

foreach (KeyValuePair<string, string> d in dlist)
{
....
if this is not looking ugly ....

...ab
http://joehacker.blogspot.com
 
You should not code C++ style in C# (or C# style in C++).

No I'm not at all coding in C++ style here, its just a matter of having
to uselessly write so much! Some languages have just made the right
decisions in some things, if we are already following (in the things
that we r following), we should follow the nice logical things/features.
While I think I know where u are coming from when you say "Given the way
C# and .NET works ..", but I don’t think that this is one of those
cases, imo.

...
List<KeyValuePair<string, string>> dlist = (List<KeyValuePair<string,
string>>)Session["dislist"];
dddis.Items.Clear();

foreach (KeyValuePair<string, string> d in dlist)
{
...
if this is not looking ugly ....

First of all, some people prefer to use "var" for variable declarations
where the type can be inferred. In your above code example, you would
only need the type specified in the actual cast of the value used to
initialize the variable "dlist". The type of "dlist" and "d" could both
be var, eliminating typing.

Secondly, given that there is no preprocesser in C#, I really don't see
what you think could be added to C# that provides better functionality
along the lines of what you want than the existing type aliasing feature I
described earlier.

Third, if you would prefer to add a reference to an external file, rather
than simply writing the alias directive, and it's not important to you for
the type used to be _exactly_ the type with the lengthy name (and this
should normally be the case), you could create a project in which a new
class defined along the lines of Scott's suggestion is declared, and then
just use _that_ class instead.

The statement "some languages have just made the right decisions" strongly
implies that C# has been designed poorly, and frankly that's just a bunch
of hooey. There is absolutely nothing wrong with the design of C# in this
particular area; it just happens to be the way C# works, having been
designed for specific goals that are incompatible with the approach C++
takes.

No language can have everything. C# has a lot of things that C++ doesn't
have, and in large part _because_ of design decisions in the language that
result in this kind of difference existing.

If you want a language that works like C++, use C++.

Pete
 
First of all, some people prefer to use "var" for variable declarations
where the type can be inferred. In your above code example, you would
only need the type specified in the actual cast of the value used to
initialize the variable "dlist". The type of "dlist" and "d" could both
be var, eliminating typing.

I mentioned in my original post that I am coding in C#2k5 (framework version
2.0.50727?). I believe we don’t have var in there. If we have that I'm going
to look for it and use, its going to be awesome.
Third, if you would prefer to add a reference to an external file, rather
than simply writing the alias directive, and it's not important to you for
the type used to be _exactly_ the type with the lengthy name (and this
should normally be the case), you could create a project in which a new
class defined along the lines of Scott's suggestion is declared, and then
just use _that_ class instead.

sorry but I cannot see Scott's post anywhere. It showed up on my news client
(windows live mail for win7 rc) and *disappeared* when I refreshed, I don’t
know how. It appeared as strikedout briefly.
............ and it's not important to you for the type used to be
_exactly_ the type with the lengthy name (and this should normally be the
case), .................

Thanks for the advice, but I don’t see how "this should normally be the
case", I think it goes from programmer to programmer.
The statement "some languages have just made the right decisions" strongly
implies that C# has been designed poorly, and frankly that's just a bunch
of hooey. ...

had absolutely no such intentions :) peace!
No language can have everything. C# has a lot of things that C++ doesn't
There is absolutely nothing wrong with the design of C# in this particular
area

my words were "Some languages have just made the right decisions in some
things" (the ending 2 words), see I'm not saying any one language is
perfect, that’s never possible and I know that. It was just one thing
related to how that particular declaration required so much typing, and I
called it "useless", and I strongly believe that a lot of people would have
the same opinion (even if 50% of the C# devs think otherwise). Since u
mentioned "var" I think that can be one of the good solutions. So we already
have it, just not in the version that I'm using right now.
Secondly, given that there is no preprocesser in C#, I really don't see
what you think could be added to C# that provides better functionality

listen if they want to provide such a feature, they will, they wont be
constrained by the preprocessor abilities not being there, and when they do
everyone is immediately going to start loving it.

Again I think Var is it! And I heard it was introduced becuz of the Linq
extensions to the language. God bless Linq, otherwise ppl like me would have
had a tough time trying to convince everyone that it is needed!

thanks.

Peter Duniho said:
You should not code C++ style in C# (or C# style in C++).

No I'm not at all coding in C++ style here, its just a matter of having
to uselessly write so much! Some languages have just made the right
decisions in some things, if we are already following (in the things
that we r following), we should follow the nice logical things/features.
While I think I know where u are coming from when you say "Given the way
C# and .NET works ..", but I don’t think that this is one of those
cases, imo.

...
List<KeyValuePair<string, string>> dlist = (List<KeyValuePair<string,
string>>)Session["dislist"];
dddis.Items.Clear();

foreach (KeyValuePair<string, string> d in dlist)
{
...
if this is not looking ugly ....

First of all, some people prefer to use "var" for variable declarations
where the type can be inferred. In your above code example, you would
only need the type specified in the actual cast of the value used to
initialize the variable "dlist". The type of "dlist" and "d" could both
be var, eliminating typing.

Secondly, given that there is no preprocesser in C#, I really don't see
what you think could be added to C# that provides better functionality
along the lines of what you want than the existing type aliasing feature I
described earlier.

Third, if you would prefer to add a reference to an external file, rather
than simply writing the alias directive, and it's not important to you for
the type used to be _exactly_ the type with the lengthy name (and this
should normally be the case), you could create a project in which a new
class defined along the lines of Scott's suggestion is declared, and then
just use _that_ class instead.

The statement "some languages have just made the right decisions" strongly
implies that C# has been designed poorly, and frankly that's just a bunch
of hooey. There is absolutely nothing wrong with the design of C# in this
particular area; it just happens to be the way C# works, having been
designed for specific goals that are incompatible with the approach C++
takes.

No language can have everything. C# has a lot of things that C++ doesn't
have, and in large part _because_ of design decisions in the language that
result in this kind of difference existing.

If you want a language that works like C++, use C++.

Pete
 
Secondly, given that there is no preprocesser in C#, I really don't see
what you think could be added to C# that provides better functionality
along the lines of what you want than the existing type aliasing feature I
described earlier.

You are confusing typedef with #define.
typedef is not a preprocessor thing, it is part of the language.
http://en.wikipedia.org/wiki/Typedef

It not only saves typing, but also makes intent clear
Instead of List<KeyValuePair<string, string>> you can call that "Dictionary"

It also helps writing clean code.
If later you decide that the performance is poor, you can replace
the list of keyvaluepair with a has table, and most of the code will not
change.
Imagine changing 20 functions that take a List<KeyValuePair<string, string>>
as parameter instead of Dictionary.

var was created to reduce some of that pain.
Is nice to have, good sometimes, but it is not the always the right solution.


I am not attacking C# here, just helping with some of the misunderstanding
you seem to have about typedef.
 
You are confusing typedef with #define.
typedef is not a preprocessor thing, it is part of the language.
http://en.wikipedia.org/wiki/Typedef

Sorry, yes. When one is away from a language for long enough, the
subtleties become hazy.

My point about the preprocessor is that the preprocessor is intrinsically
linked with the concept of include files in C++, and the lack of an
include file is the only thing missing from C# that doesn't allow quite
the same convenience that a typedef in C++ allows. Type aliasing is
basically the same feature, but without include files it has to be
specified in each source file where it's used (not necessarily a bad
thing, actually).
It not only saves typing, but also makes intent clear
Instead of List<KeyValuePair<string, string>> you can call that
"Dictionary"

Actually, that specific example is a really BAD thing. There's already a
"Dictionary" type in .NET. Calling some other type "Dictionary" is going
to cause maintenance headaches. Especially since a
List<KeyValuePair<string, string>> really isn't actually a dictionary (no
hash-based lookup, which "dictionary" implies in the context of .NET).

The example would be more productive with a different alias name as the
example.
It also helps writing clean code.
If later you decide that the performance is poor, you can replace
the list of keyvaluepair with a has table, and most of the code will not
change.

If one needs to abstract away implementation details like that, the
solution is to create a whole new class that does that. Simply aliasing
an existing type is again going to be a maintenance problem.

Don't get me wrong. I'm not saying type aliasing doesn't have its place
and I don't object to the fact that you can do it in C#. It's just that
above are IMHO poorly chosen examples.
Imagine changing 20 functions that take a List<KeyValuePair<string,
string>>
as parameter instead of Dictionary.

Been there, done that. But you can't just safely change the type out of
underneath some code anyway. The retyping of a type name is the least of
your problems. You have to revisit each use of the aliased type and make
sure it's still being used correctly.

Again, it's much better to simply encapsulate implementation details like
that in a separate class. That will keep any changes localized and
maintainable.
var was created to reduce some of that pain.
Is nice to have, good sometimes, but it is not the always the right
solution.

Actually, "var" was in fact created to support anonymous types, which in
turn are primary to support LINQ. I seriously doubt any member of the
language design team felt that reducing the number of characters typed was
in any way a significantly influential factor in the decision to add "var"
to the language.
I am not attacking C# here, just helping with some of the
misunderstanding
you seem to have about typedef.

Yes, thank you for the correction. It doesn't really change my points
about type aliasing in C#, but it's good to get the facts straight.

Pete
 
I mentioned in my original post that I am coding in C#2k5 (framework
version 2.0.50727?). I believe we don’t have var in there. If we have
that I'm going to look for it and use, its going to be awesome.

You're right, it's new to C# 3.0.
sorry but I cannot see Scott's post anywhere. It showed up on my news
client (windows live mail for win7 rc) and *disappeared* when I
refreshed, I don’t know how. It appeared as strikedout briefly.

He simply suggested inheriting your List<KeyValuePair<string, string>>
class in a new class, and then using that new class instead of
List<KeyValuePair<string, string>> directly.

It's more up-front work, and it changes the actual reflected type, but it
can still work fine in most situations.
Thanks for the advice, but I don’t see how "this should normally be the
case", I think it goes from programmer to programmer.

No. Most programs should not care whether the exact type you're using is
List<KeyValuePair<string, string>> or some type that inherits
List<KeyValuePair<string, string>>. The only time it would come up is if
you need to compare the actual reflected type to some specific type, and
even then only if there's some specific requirement that the reflected
[...]
Secondly, given that there is no preprocesser in C#, I really don't see
what you think could be added to C# that provides better functionality

listen if they want to provide such a feature, they will, they wont be
constrained by the preprocessor abilities not being there, and when they
do everyone is immediately going to start loving it.

You don't seem to be comprehending. C# already has type aliasing that
works just like typedef. The only difference is the lack of include
files, something a preprocessor would handle.

I'm one of the last people to be able to predict what the language
designers might add, but I really doubt you will ever see include files in
C#. The lack of include files was a specific design choice made very
early in the language's development and it's fairly fundamental to the
philosophies behind C#.
Again I think Var is it! And I heard it was introduced becuz of the Linq
extensions to the language. God bless Linq, otherwise ppl like me would
have had a tough time trying to convince everyone that it is needed!

I remain puzzled as to you why don't just use type aliasing as it exists
in C# today. But, whatever.

Pete
 
I'm now using type aliasing as you suggested. But I would have loved it if
they had designed it in a way so that I could declare it at one place and
use it everywhere. We can declare delegate at one place and use it
everywhere, it doesn’t have to be made possible through include files (I don’t
want and never requested include files in C#). Whenever we are restricted by
any feature, we do still use it cuz that’s what we got.
I'm using type aliasing, but now the maintenance is there and I don’t have
any 1 place where I could just declare it and it immediately becomes
available everywhere. Much needed! Now when I talk about this, you are going
to repeat the thing suggested by Scott. I got that! If devs want to come up
with workarounds for the missing features in a programming langs, they do,
but that’s not a *problem solved*. We had workarounds suggested for generics
when they were not in the language (was that an extreme
example/comparison?).

Function pointers in c++ and delegates in C# are so different, but yet C#
has them molded according to what feels native to C#. The same thing could
have been done with typedefs.

my programming experience maybe lacking something that I'm unable to get
your point. But I feel whatever I can want in a language and request here,
99% would be responded as workarounds or some principle of software
engineering (yuck) would be dictated to represent the point of view of what
the poster thinks is right. After all, most are opinions, not equations that
either are doing the right things or wrong.

Just an example coming to my mind: Should I use anonymous classes just bcuz
they are made possible through Linq? Does javascript has linq thats why they
have anonymous classes? As far as my experience with it goes, anonymous
classes make me write code very fast which otherwise seems so laborious
(going and declaring a class, making a ctor to receive all the parameters
and than assigning them to the local variable, and than a question of should
I make them available as public instance variables or through properties).
So anonymous classes to me feels like fresh air without ever using them with
linq.

just my point three seven five nine seven cents!!!

Peter Duniho said:
I mentioned in my original post that I am coding in C#2k5 (framework
version 2.0.50727?). I believe we don’t have var in there. If we have
that I'm going to look for it and use, its going to be awesome.

You're right, it's new to C# 3.0.
sorry but I cannot see Scott's post anywhere. It showed up on my news
client (windows live mail for win7 rc) and *disappeared* when I
refreshed, I don’t know how. It appeared as strikedout briefly.

He simply suggested inheriting your List<KeyValuePair<string, string>>
class in a new class, and then using that new class instead of
List<KeyValuePair<string, string>> directly.

It's more up-front work, and it changes the actual reflected type, but it
can still work fine in most situations.
Thanks for the advice, but I don’t see how "this should normally be the
case", I think it goes from programmer to programmer.

No. Most programs should not care whether the exact type you're using is
List<KeyValuePair<string, string>> or some type that inherits
List<KeyValuePair<string, string>>. The only time it would come up is if
you need to compare the actual reflected type to some specific type, and
even then only if there's some specific requirement that the reflected
[...]
Secondly, given that there is no preprocesser in C#, I really don't see
what you think could be added to C# that provides better functionality

listen if they want to provide such a feature, they will, they wont be
constrained by the preprocessor abilities not being there, and when they
do everyone is immediately going to start loving it.

You don't seem to be comprehending. C# already has type aliasing that
works just like typedef. The only difference is the lack of include
files, something a preprocessor would handle.

I'm one of the last people to be able to predict what the language
designers might add, but I really doubt you will ever see include files in
C#. The lack of include files was a specific design choice made very
early in the language's development and it's fairly fundamental to the
philosophies behind C#.
Again I think Var is it! And I heard it was introduced becuz of the Linq
extensions to the language. God bless Linq, otherwise ppl like me would
have had a tough time trying to convince everyone that it is needed!

I remain puzzled as to you why don't just use type aliasing as it exists
in C# today. But, whatever.

Pete
 
............ and it's not important to you for the type used to be
No. Most programs should not care whether the exact type you're
using is List<KeyValuePair<string, string>> or some type that inherits
List<KeyValuePair<string, string>>. The only time it would come up
is if you need to compare the actual reflected type to some specific
type, and even then only if there's some specific requirement that
the reflected type compare as equal to List<KeyValuePair<string,
string>>. That really is a very narrow set of requirements. Normally they
won't be met.

Actually, the code shown, which can be shortened to:

void f(object x)
{
List<Something<More<T,S>,U>> lst = (List<Something<More<T,S>,U>>)x;
foreach (Something<More<T,S>,U> elem in lst) { ... }
}

will behave very badly if you try to use derivation as a typedef. The cast
would fail. The foreach would return no results. etc.
 
If one needs to abstract away implementation details like that, the
solution is to create a whole new class that does that. Simply
aliasing an existing type is again going to be a maintenance problem.

Don't get me wrong. I'm not saying type aliasing doesn't have its
place and I don't object to the fact that you can do it in C#. It's
just that above are IMHO poorly chosen examples.


Been there, done that. But you can't just safely change the type out
of underneath some code anyway. The retyping of a type name is the
least of your problems. You have to revisit each use of the aliased
type and make sure it's still being used correctly.

Well, generics do a pretty good job of that, and provide constraints. I
wish there was some sort of module-wide generics capability too. You can
sort-of get this by turning your namespace into a generic static class, then
configuring the generic would look something like:

static class Program
{
public static void Main(string[] args) { MyNamespace<ActualClass1,
ActualClass2>.Main(); }
}
 
Peter Duniho said:
First of all, some people prefer to use "var" for variable declarations
where the type can be inferred. In your above code example, you would
only need the type specified in the actual cast of the value used to
initialize the variable "dlist". The type of "dlist" and "d" could both
be var, eliminating typing.

Yeah, but some people don't like it....and doesn't do you any good for
passing parameters.
Secondly, given that there is no preprocesser in C#, I really don't see
what you think could be added to C# that provides better functionality
along the lines of what you want than the existing type aliasing feature I
described earlier.

Something along the lines of what you described, but at the namespace
level.

This should have been introduced along with generics. I'd agree that
before that there was little to no reason to use it, but once generics
*were* introduced there is a need for a typedef equivalent.
 
Actually, the code shown, which can be shortened to:

void f(object x)
{
List<Something<More<T,S>,U>> lst = (List<Something<More<T,S>,U>>)x;
foreach (Something<More<T,S>,U> elem in lst) { ... }
}

will behave very badly if you try to use derivation as a typedef. The
cast
would fail. The foreach would return no results. etc.

You should post a concise-but-complete code example that unambiguously
demonstrates what you're talking about. As it is, your comment is too
vague for me to make sense of it. I don't doubt what you wrote is true,
but without enough context it's impossible to know for sure why, never
mind why it should relate to this particular discussion.

It is true that pre-C# 4.0, lack of variance between generic types would
interfere with casting between generics that don't have exactly the same
type parameters. But a) that doesn't really apply to this particular
discussion (we're dealing with concrete types made from generics...no type
parameter variance needed), and b) in C# 4.0 support for variance will
help address a lot of the concerns related to variance anyway.

See below for a concise-but-complete code example that demonstrates use of
Scott's suggestion (i.e. inheritance as a way of typedef-ing). No compile
or run-time errors involved.

Pete


using System;
using System.Collections.Generic;

namespace TestCastTypedefInherit
{
class Program
{
class MyList : List<KeyValuePair<string, string>>
{
}

static void Main(string[] args)
{
MyList list = new MyList();

list.Add(new KeyValuePair<string, string>("key1", "value1"));
list.Add(new KeyValuePair<string, string>("key2", "value2"));
list.Add(new KeyValuePair<string, string>("key3", "value3"));

object obj = list;
List<KeyValuePair<string, string>> list2 =
(List<KeyValuePair<string, string>>)obj;

foreach (KeyValuePair<string, string> kvp in list2)
{
Console.WriteLine(kvp.Key + ", " + kvp.Value);
}

Console.ReadLine();
}
}
}
 
Peter Duniho said:
Actually, the code shown, which can be shortened to:

void f(object x)
{
List<Something<More<T,S>,U>> lst = (List<Something<More<T,S>,U>>)x;
foreach (Something<More<T,S>,U> elem in lst) { ... }
}

will behave very badly if you try to use derivation as a typedef. The
cast
would fail. The foreach would return no results. etc.

You should post a concise-but-complete code example that unambiguously
demonstrates what you're talking about. As it is, your comment is too
vague for me to make sense of it. I don't doubt what you wrote is true,
but without enough context it's impossible to know for sure why, never
mind why it should relate to this particular discussion.

It is true that pre-C# 4.0, lack of variance between generic types would
interfere with casting between generics that don't have exactly the same
type parameters. But a) that doesn't really apply to this particular
discussion (we're dealing with concrete types made from generics...no type
parameter variance needed), and b) in C# 4.0 support for variance will
help address a lot of the concerns related to variance anyway.

See below for a concise-but-complete code example that demonstrates use of
Scott's suggestion (i.e. inheritance as a way of typedef-ing). No compile
or run-time errors involved.

Pete


using System;
using System.Collections.Generic;

namespace TestCastTypedefInherit
{
class Program
{
class MyList : List<KeyValuePair<string, string>>
{
}

static void Main(string[] args)
{
MyList list = new MyList();

list.Add(new KeyValuePair<string, string>("key1", "value1"));
list.Add(new KeyValuePair<string, string>("key2", "value2"));
list.Add(new KeyValuePair<string, string>("key3", "value3"));

object obj = list;
List<KeyValuePair<string, string>> list2 =
(List<KeyValuePair<string, string>>)obj;

foreach (KeyValuePair<string, string> kvp in list2)
{
Console.WriteLine(kvp.Key + ", " + kvp.Value);
}

Console.ReadLine();
}
}
}

well I think in the spirit of solving this problem, you would first start
typdefing/inheriting KeyValuePair<string, string> itself, which is not even
possible cuz KeyValuePair is not inheritable. An annoyance for sure.
 
It is true that pre-C# 4.0, lack of variance between generic types
would interfere with casting between generics that don't have
exactly the same type parameters. But a) that doesn't really apply
to this particular discussion (we're dealing with concrete types
made from generics...no type parameter variance needed), and b) in
C# 4.0 support for variance will help address a lot of the concerns
related to variance anyway. See below for a concise-but-complete code
example that demonstrates
use of Scott's suggestion (i.e. inheritance as a way of
typedef-ing). No compile or run-time errors involved.

Pete


using System;
using System.Collections.Generic;

namespace TestCastTypedefInherit
{
class Program
{
class MyList : List<KeyValuePair<string, string>>
{
}

static void Main(string[] args)
{
MyList list = new MyList();

list.Add(new KeyValuePair<string, string>("key1",
"value1")); list.Add(new KeyValuePair<string,
string>("key2", "value2")); list.Add(new
KeyValuePair<string, string>("key3", "value3")); object obj = list;
List<KeyValuePair<string, string>> list2 =
(List<KeyValuePair<string, string>>)obj;

foreach (KeyValuePair<string, string> kvp in list2)
{
Console.WriteLine(kvp.Key + ", " + kvp.Value);
}

Console.ReadLine();
}
}
}

well I think in the spirit of solving this problem, you would first
start typdefing/inheriting KeyValuePair<string, string> itself, which
is not even possible cuz KeyValuePair is not inheritable. An
annoyance for sure.

For sure. But what I was trying to say is, you can't cast to a derived type
when you didn't create the object. And you can't rewrite every library in
existance that acts on List<Something> to use your "typedef" type instead.

using System;
using System.Collections.Generic;

namespace TestCastTypedefInherit
{
static class ImportPlugin
{
object DeSerialize(BinaryReader source)
{
// test stub for deserializing a List<KeyValuePair<string,
string>> from a BinaryReader
// deserialization code not implemented, because I don't
have it memorized

List<KeyValuePair<string, string>> list = new
List<KeyValuePair<string, string>>();

list.Add(new KeyValuePair<string, string>("key1",
"value1")); list.Add(new KeyValuePair<string, string>("key2", "value2"));
list.Add(new KeyValuePair<string, string>("key3", "value3"));
}

}

class Program
{
class MyList : List<KeyValuePair<string, string>>
{
}

static void Main(string[] args)
{
object obj = ImportPlugin.DeSerialize(null);

// try to use the returned list
// a typedef makes this code shorter
MyList list2 = (MyList)obj; // oh noes! InvalidCastException

foreach (KeyValuePair<string, string> kvp in list2)
{
Console.WriteLine(kvp.Key + ", " + kvp.Value);
}

Console.ReadLine();
}
}
}
 
For sure. But what I was trying to say is, you can't cast to a derived
type
when you didn't create the object. And you can't rewrite every library
in
existance that acts on List<Something> to use your "typedef" type
instead.

Ah, okay. I understand and agree.
 
Actually, that specific example is a really BAD thing. There's already a
"Dictionary" type in .NET.

Agree. I am not familiar with all the stuff in .NET

Been there, done that. But you can't just safely change the type out of
underneath some code anyway. The retyping of a type name is the least of
your problems. You have to revisit each use of the aliased type and make
sure it's still being used correctly.

If you designed properly from the beginning it is not a major problem.
It is painless with most of the stl containers.
 
Back
Top