Hi, I know Generics are strong-typed collection but after seeing below example I am bit confused, probably I am missing something here. Can anyone please justify:
List<object> obj = new List<object>(); int i = 0; obj.Add(i); obj.Add(123); obj.Add("Hello");
Note that the use of the object type is not considered strongly-type. Thus, a generic type based on the object type would technically not be strongly typed. Still, all of the elements in your above List collection are still of type object, so, in a passive
way, it is strongly typed.
This would also be true if you used the dynamic type with generics.
Christopher Reed, MCT, MCPD, MCTS, Microsoft Specialist, MTA
"The oxen are slow, but the earth is patient."
for (int objIndex = 0; objIndex < obj.Count; objIndex++)
{
Console.WriteLine ("value {0}, type {1}", obj[objIndex], obj[objIndex].GetType());
}
output:
value 0, type System.Int32
value 123, type System.Int32
value Hello, type System.String.
Again, this all works because "You can assign values of any type to variables of typeobject".
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
I should say that it is strong typed with object type.
Not really. Let me explain ... Christopher Reed (http://forums.asp.net/post/5256007.aspx) put it corretly imho when he wrote "the use of the object type is not considered strongly-type".
Represents a strongly typed list of
objects that can be accessed by index.
Provides methods to search, sort, and manipulate lists.
Above is where the confusion begins. Please note that i am not a c# expert like Jon Skeet, Joe Albahari, Anders Hejlsberg, et al.
Generally, some languages are exclusively strongly typed (c#, until relatively recently, was such a language) while others are exclusively
not strongly typed.
Put simply, strong typing is when the determination of type does not need to be
dynamically performed ... i.e., it's done at compile time rather than at run time.
Edit: of course, one could argue that at compile time, the compiler "knows" that the type of "object" is System.Object; a reasonable counter argument imho is that
object because of its for all intents and purposes dynamic behavour is therefore
notstrongly typed ... bottom line, it's simply easier to consider
System.Object as the exception to the rule, i.e., because
System.Object is the ultimate base class of
all classes, System.Object gets the special privilege of being allowed to be in the same arena as its
strongly typed cousins. end Edit.
Let's look at a variation of your code:
List<System.String> obj = new List<System.String>();
obj.Add("Hello");
int i = 0;
obj.Add(i); // fails to compile
obj.Add(123); // fails to compile
the last two lines get this error: The best overloadedmethod match for 'System.Collections.Generic.List<string>.Add(string)' has some invalid arguments Argument 1: cannot convert from 'int' to 'string'
We can fix the above code by appending .ToString():
List<System.String> obj = new List<System.String>();
obj.Add("Hello");
int i = 0;
obj.Add(i.ToString());
obj.Add(123.ToString());
for (int objIndex = 0; objIndex < obj.Count; objIndex++)
{
Console.WriteLine ("value {0}, type {1}", obj[objIndex], obj[objIndex].GetType());
}
value Hello, type System.String
value 0, type System.String
value 123, type System.String
as you can see, and as expected, all three values are type System.String.
So, bottom line, imho, it's fair to conclude that System.Object is
not strongly typed; OTOH, System.Object is a class, and a class is for all intents and purposes, at type ... for that reason, one can legally replace
T in List<T> with System.Object or its c# alias
object.
best wishes for 2013 ... gerry
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
gurunguns
Member
72 Points
209 Posts
Are Generics really strong-Typed collection ?
Jan 01, 2013 04:34 AM|LINK
Hi, I know Generics are strong-typed collection but after seeing below example I am bit confused, probably I am missing something here. Can anyone please justify:
List<object> obj = new List<object>();
int i = 0;
obj.Add(i);
obj.Add(123);
obj.Add("Hello");
Save Green.
Careed
All-Star
18774 Points
3637 Posts
Re: Are Generics really strong-Typed collection ?
Jan 01, 2013 05:50 AM|LINK
Note that the use of the object type is not considered strongly-type. Thus, a generic type based on the object type would technically not be strongly typed. Still, all of the elements in your above List collection are still of type object, so, in a passive way, it is strongly typed.
This would also be true if you used the dynamic type with generics.
"The oxen are slow, but the earth is patient."
gerrylowry
All-Star
20525 Points
5713 Posts
Re: Are Generics really strong-Typed collection ?
Jan 01, 2013 06:16 AM|LINK
@ gurunguns
what is your confusion?
http://msdn.microsoft.com/en-us/library/system.object.aspx
"Object Class", .NET Framework 4.5
Supports all classes in the .NET Framework class hierarchy and
provides low-level services to derived classes.
This is the ultimate base class of all classes in the .NET Framework;
it is the root of the type hierarchy.
http://msdn.microsoft.com/en-us/library/9kkx3h3c(v=vs.110).aspx
"object (C# Reference)"
"The object type is an alias for Object in the .NET Framework."
"You can assign values of any type to variables of type object."
http://msdn.microsoft.com/en-us/library/system.object.gettype.aspx
"Object.GetType Method"
gurunguns, add these lines after your code:
for (int objIndex = 0; objIndex < obj.Count; objIndex++) { Console.WriteLine ("value {0}, type {1}", obj[objIndex], obj[objIndex].GetType()); }output:
Again, this all works because "You can assign values of any type to variables of type object".
g.
gurunguns
Member
72 Points
209 Posts
Re: Are Generics really strong-Typed collection ?
Jan 01, 2013 11:14 AM|LINK
Thanks gerrylowry for the detailed response. yes I understand object is the base class for all controls.
So, I should say that it is strong typed with object type.
Save Green.
gurunguns
Member
72 Points
209 Posts
Re: Are Generics really strong-Typed collection ?
Jan 01, 2013 11:17 AM|LINK
I just tried below sample
List<DemoBase> objDemo = new List<DemoBase>();
objDemo.Add(new DemoBase { Name = "A", Deppt = "A_D" });
objDemo.Add(new DemoSub { NameSub = "A", DepptSub = "A_D" });
where DemoBase being the base class for DemoSub, which I guess matches the above scenario.
Save Green.
gerrylowry
All-Star
20525 Points
5713 Posts
Re: Are Generics really strong-Typed collection ?
Jan 01, 2013 03:46 PM|LINK
@ gurunguns
Not really. Let me explain ... Christopher Reed (http://forums.asp.net/post/5256007.aspx) put it corretly imho when he wrote "the use of the object type is not considered strongly-type".
First, we need to look at List<T> here:
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
"List<T> Class", .NET Framework 4.5
Represents a strongly typed list of objects that can be accessed by index.
Provides methods to search, sort, and manipulate lists.
Above is where the confusion begins. Please note that i am not a c# expert like Jon Skeet, Joe Albahari, Anders Hejlsberg, et al.
Generally, some languages are exclusively strongly typed (c#, until relatively recently, was such a language) while others are exclusively not strongly typed.
Put simply, strong typing is when the determination of type does not need to be dynamically performed ... i.e., it's done at compile time rather than at run time.
Edit: of course, one could argue that at compile time, the compiler "knows" that the type of "object" is System.Object; a reasonable counter argument imho is that object because of its for all intents and purposes dynamic behavour is therefore not strongly typed ... bottom line, it's simply easier to consider System.Object as the exception to the rule, i.e., because System.Object is the ultimate base class of all classes, System.Object gets the special privilege of being allowed to be in the same arena as its strongly typed cousins. end Edit.
Let's look at a variation of your code:
List<System.String> obj = new List<System.String>(); obj.Add("Hello"); int i = 0; obj.Add(i); // fails to compile obj.Add(123); // fails to compilethe last two lines get this error:
The best overloaded method match for
'System.Collections.Generic.List<string>.Add(string)'
has some invalid arguments
Argument 1: cannot convert from 'int' to 'string'
We can fix the above code by appending .ToString():
List<System.String> obj = new List<System.String>(); obj.Add("Hello"); int i = 0; obj.Add(i.ToString()); obj.Add(123.ToString()); for (int objIndex = 0; objIndex < obj.Count; objIndex++) { Console.WriteLine ("value {0}, type {1}", obj[objIndex], obj[objIndex].GetType()); }note how the output, as is to be expected, is slightly different from http://forums.asp.net/post/5256020.aspx:
as you can see, and as expected, all three values are type System.String.
So, bottom line, imho, it's fair to conclude that System.Object is not strongly typed; OTOH, System.Object is a class, and a class is for all intents and purposes, at type ... for that reason, one can legally replace T in List<T> with System.Object or its c# alias object.
best wishes for 2013 ... gerry