Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Sunday, March 13, 2011 3:27 PM
The first line uses polymorphism but want I want to know is the relation of object and Object, I assume that it derives/inherits from Object in order use polymorphism.
private void button1_Click(object sender, EventArgs e)
{
((Button)sender).Text = "Clicked!";
Button newButton = new Button();
newButton.Text = "New Button!";
//some codes
}
All replies (46)
Sunday, March 13, 2011 4:16 PM ✅Answered | 1 vote
Hi,
you are interested in the difference between object and Object in C#.
a) object is a c# keyword. Please see http://msdn.microsoft.com/en-us/library/9kkx3h4c(v=VS.100).aspx for details (It is just an alias for Object).
b) Object is a class that all other classes are derived from.
With kind regards,
Konrad
Tuesday, March 15, 2011 9:42 AM ✅Answered | 1 vote
Just to make it clear: There is NO semantic difference whatsoever between 'object' and 'System.Object'.
Sunday, March 13, 2011 3:33 PM
The colors in the IDE indicate keywords belonging to the C# language by blue and those belonging to the .NET framework by light green. Intellisense explains the derivation of the keywords.
Tuesday, March 15, 2011 8:43 AM
Hi Randelramirez1,
Any update about this issue?
Konrad's reply is comprehensive for special classes derived from Object. Such as String and string.
If you have any other questions about it, please feel free to post here.
Best Regards,
Cookie Luo[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Tuesday, March 15, 2011 9:51 AM
Of course there is a difference.
System Object is a class, like Konrad already wrote, it is the base of all other Classes in .Net.
An object is a piece of code (ILS) residence somewhere in memory and as it is a reference type object, on the managed heap, but all derived in whatever way from System.Object.
Success
Cor
Tuesday, March 15, 2011 11:48 AM
Cor, you are pointing out a complete new view that I missed so far.
Maybe "object" simply meand the wording useg between developers. Then an object is an instance of a class. An object is just an area of memory that contains data. There is no code (ILS) involved! There are even memory protection systems that make sure, that the memory area will never be executed! But of course: The memory area is connected to a type and the type has code behind it. But the code belongs to the class of course and not to the instance.
When refering to the C# keyword object: That is simply an Alias to System.Object. So there is no difference in code if you use object or System.Object.
With kind regards,
Konrad
Tuesday, March 15, 2011 3:04 PM
Konrad,
Did I deny what you wrote, however I did not read "what is the difference between Object and the keyword object"
But there is in my perception a difference between an object as used in the way of C# and the system type Object.
The word object is a difficult word to describe.
http://en.wikipedia.org/wiki/Object
In my opinion is the relation for the meaning of object and the keyword object to often done.
By the way there is some code involved, you cannot hold anything in memory if don't create something to reference to that, then it is only a waste of memory.
(I've specially not written about value types, structs, static classes and instanced classes to make in not even more confusing).
Success
Cor
Tuesday, March 15, 2011 3:31 PM
Cor, I think I understood you. That is the reason why I wrote "you are pointing out a complete new view that I missed so far."
I just thought more about that and simply got confused a little bit about your "An object is a piece of code (ILS) residence somewhere in memory". I simply wanted to add some information about this, because that is not fully true in my eyes. That is all.
But I gave the information that I wanted to give and that is already all.
Konrad
Tuesday, March 15, 2011 4:04 PM
Of course there is a difference.
System Object is a class, like Konrad already wrote, it is the base of all other Classes in .Net.
An object is a piece of code (ILS) residence somewhere in memory and as it is a reference type object, on the managed heap, but all derived in whatever way from System.Object.
Success
Cor
You misunderstand.
I'm saying that there is no semantic difference between the C# keyword "object" and .Net's "System.Object".
There is no difference whatsoever. If you inspect the IL generated for any C# code that uses the keyword "object" you'll see that it is translated into "System.Object".
I think it was fairly clear from the OP that we are talking about the C# keyword "object", since you can see that he is using the "object" keyword in the question.
I would also have thought that it would be fairly obvious that I was talking about the C# "object" keyword rather than some abstract term... Otherwise what I typed wouldn't make any grammatical sense!
Tuesday, March 15, 2011 5:22 PM
In my opinion is the relation for the meaning of object and the keyword object to often done.
By the way there is some code involved, you cannot hold anything in memory if don't create something to reference to that, then it is only a waste of memory.
You're out of context pal.
context, context, context.
Eyal, Regards.
Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.
SharpHighlighter - is an extension for Visual Studio, a fairly simple code highlighter for C#.
Tuesday, March 15, 2011 6:01 PM
n it is only a waste of memory.
You're out of context pal.
context, context, context.
You are in the wrong forum, see here where your reply fits
http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/threads
Success
Cor
Wednesday, March 16, 2011 3:49 AM
@Cor, you're good community member but sometimes it seems like some of your replies are offshore , so my reply was an attempt to make sure you understand that you should reread the OP once again.
The OP wasn't speaking about the word "object" in general but specifically trying to understand whether there's a difference between an Object that is part of the BCL and an object as it appear in the language itself.
I'm at the right place, thanks for the directions.
Eyal, Regards.
Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.
SharpHighlighter - is an extension for Visual Studio, a fairly simple code highlighter for C#.
Wednesday, March 16, 2011 7:26 AM
The OP wasn't speaking about the word "object" in general but specifically trying to understand whether there's a difference between an Object that is part of the BCL and an object as it appear in the language itself.
@Eyal,
You are great you can see what somebody means without that it is said or written.
You should not be answering questions in a forum but be somewhere on stage.
You are unique man.
Be aware that it is not bad to look sometimes a step further than your village.
Success
Cor
Wednesday, March 16, 2011 7:32 AM
I think it was fairly clear from the OP that we are talking about the C# keyword "object", since you can see that he is using the "object" keyword in the question.
@Matthew
How?
In my idea is the problem for most that they make to quick a relation to the keywords or the system Class. An object in the meaning of OO has nothing to do with the type name Object, he is even talking about polymorphism so for sure not about the base class of .Net
By the way he is also using the keyword is in his question. Does that mean that I should read his question related to that keyword
Success
Cor
Wednesday, March 16, 2011 7:54 AM
You say "an object in the meaning of OO", the poster asks about the meaning in C#. Look at the title.
You also ignore the fact that the poster makes specific reference to the first line of code i.e. "private void button1_Click(object sender, EventArgs e)".
The OP is therefore clearly asking about the meaning of 'object' versus 'Object' in c# not in the general world of OO.
Therefore your comments that dismiss this view are invalid.
I put this down to one of two possible causes. Firstly, English is clearly not your first language so I suspect you do not grasp that in many cases there are clear inferences that can (and have to) be made. These derive from the context, in this case the OP's title, the fact that this is a C# forum and the reference to the first line.
Secondly, may be you are just 'reading out' what clearly needs to be read in as context.
Regards David R "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.
Wednesday, March 16, 2011 9:41 AM
I think it was fairly clear from the OP that we are talking about the C# keyword "object", since you can see that he is using the "object" keyword in the question.
@Matthew
How?
In my idea is the problem for most that they make to quick a relation to the keywords or the system Class. An object in the meaning of OO has nothing to do with the type name Object, he is even talking about polymorphism so for sure not about the base class of .Net
By the way he is also using the keyword is in his question. Does that mean that I should read his question related to that keyword
Success
Cor
The anwser to your "how" is simply that from the context, it is pretty clear that the OP is asking about the difference between the "object" keyword and "System.Object".
It may not be clear to you, but it certainly is to me. :)
Wednesday, March 16, 2011 9:50 AM
@Eyal,
You are great you can see what somebody means without that it is said or written.
You should not be answering questions in a forum but be somewhere on stage.
You are unique man.
Be aware that it is not bad to look sometimes a step further than your village.
When I wrote my post I wasn't trying to be cynical but sincere.
When someone is asking a question one should answer it in the context it was given and go from there.
I didn't said anything about you personally I'd expect you to do the same and respond more maturely.
In your post you stated that there's a difference but this wasn't the kind of difference the OP expects to understand.
While it's not bad to step out of the village and see the jungle, it's not so wise either, all by myself ? :(
Eyal, Regards.
Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.
SharpHighlighter - is an extension for Visual Studio, a fairly simple code highlighter for C#.
Wednesday, March 16, 2011 10:00 AM
@Eyal: Cor is well-known for wilfully misinterpreting posts, and responding in the kind of defensive way that you see above. I think of him as the "rogue MVP". :)
Wednesday, March 16, 2011 10:18 AM
@Matthew: Yes, I know, it's unfortunate. :p
Eyal, Regards.
Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.
SharpHighlighter - is an extension for Visual Studio, a fairly simple code highlighter for C#.
Wednesday, March 16, 2011 12:43 PM
@Eyal: Cor is well-known for wilfully misinterpreting posts, and responding in the kind of defensive way that you see above. I think of him as the "rogue MVP". :)
Maybe as you are so good in understanding post, than can you in clear English write what I have written wrong here.
Of course there is a difference.
System Object is a class, like Konrad already wrote, it is the base of all other Classes in .Net.
An object is a piece of code (ILS) residence somewhere in memory and as it is a reference type object, on the managed heap, but all derived in whatever way from System.Object.
I've not written that there is no semantic difference, I've not written that there is no difference between System.Object and written that there is a difference between the keyword object and the class System object.
I've written that there is Of course a difference, where I mean many things; for instance polymorphism. Objects are especially not always the same, they can have the same base, but a cat object is not the same as a dog object while it are both mammals.
About my English where you think you must constantly write about; I never told my English is perfect; but in this case is in my idea the pot calling the kettle black.
Also be aware that these forums are not made to blog your personal opinions about other people.
Success
Cor
Wednesday, March 16, 2011 12:59 PM
@Eyal: Cor is well-known for wilfully misinterpreting posts, and responding in the kind of defensive way that you see above. I think of him as the "rogue MVP". :)
Maybe as you are so good in understanding post, than can you in clear English write what I have written wrong here.
Of course there is a difference.
System Object is a class, like Konrad already wrote, it is the base of all other Classes in .Net.
An object is a piece of code (ILS) residence somewhere in memory and as it is a reference type object, on the managed heap, but all derived in whatever way from System.Object.
I've not written that there is no semantic difference, I've not written that there is no difference between System.Object and written that there is a difference between the keyword object and the class System object.
I've written that there is Of course a difference, where I mean many things; for instance polymorphism. Objects are especially not always the same, they can have the same base, but a cat object is not the same as a dog object while it are both mammals.
About my English where you think you must constantly write about; I never told my English is perfect; but in this case is in my idea the pot calling the kettle black.
Also be aware that these forums are not made to blog your personal opinions about other people.
Success
Cor
I'm sorry, I can't make head nor tail of that. Would it be at all possible for you to elucidate it?
I will comment on this part of it:
"Also be aware that these forums are not made to blog your personal opinions about other people."
You said this about Eyal in a completely unprovoked attack:
*"You are great you can see what somebody means without that it is said or written. *
*You should not be answering questions in a forum but be somewhere on stage. *
You are unique man."
Do you understand the term "hypocrite", perhaps? :)
Wednesday, March 16, 2011 1:53 PM
I'm sorry, I can't make head nor tail of that. Would it be at all possible for you to elucidate it?
Do you understand the term "hypocrite", perhaps? :)
And still again able to blog your personal opinion about somebody without any sentence that added to the question of the OP.
All my replies in this message thread except one to Eyal contains something that explain something about the difference of the keyword object; the class System object; and the use of an object in C# especially related to polymorphism. And I try still here to despite all odds to show to you that object does not only mean System object in the context of OOP and therefore C#, beside in the semantic of C#.
But the text you quoted was of course the one I wrote to Eyal, and still you did not understand any word I wrote.
Oh and Yes I think to know what is a "hypocrite"
Success
Cor
Wednesday, March 16, 2011 2:20 PM
The question (from the title of this thread) is: "What is the difference between Object and object".
I am stating that the OP meant this:
In the following code, what is the difference between Object and object:
void Test(string s)
{
object o1 = s; // What's the difference between 'object' here
Object o2 = s; // and 'Object' here.
...
}
The answer in this case is: There is no semantic difference.
Perhaps the OP might let us know what he meant at some point. :)
Wednesday, March 16, 2011 3:06 PM
You engineer types read too much into these types of questions. It reminds me of the very long thread between int and Int32. One is an alias, the other is the class. It is that simple. Which to use is a matter of preference and taste. I like being explicit so I always use the class not the alias. But it matters little. Matthew is right, no difference except one is an alias for the class and the other is the class itself.
John Grove, MCC - Senior Software Engineer
Wednesday, March 16, 2011 3:14 PM | 2 votes
Sorry, I just couldn't resist... Let's just agree to disagree and leave it all to a rest?
"The improbable we do, the impossible just takes a little longer." (Steven Parker)
Wednesday, March 16, 2011 3:18 PM
You engineer types read too much into these types of questions. It reminds me of the very long thread between int and Int32. One is an alias, the other is the class. It is that simple. Which to use is a matter of preference and taste. I like being explicit so I always use the class not the alias. But it matters little. Matthew is right, no difference except one is an alias for the class and the other is the class itself.
John Grove, MCC - Senior Software Engineer
I see where you are going...
So maybe, in 10 years, Microsoft will decide that object is now an alias for something else, and we'll all be in deep trouble?
Not sure if I remember the "int vs Int32" discussion correctly... :-D
EDIT: Note the smiley at the end. In no way am I making a helpful contribution to this discussion, nor am I trying to get this discussion into the "omg what if that would really happen" area, nor am I trying to defend or offend the quoted person... (just to be safe...)
"The improbable we do, the impossible just takes a little longer." (Steven Parker)
Wednesday, March 16, 2011 3:20 PM
If that is the case then my programming style in which I am explicit will have saved me :-)
John Grove, MCC - Senior Software Engineer
Wednesday, March 16, 2011 3:40 PM
Not sure if I remember the "int vs Int32" discussion correctly... :-D
Do you dare me to go and bump it? ;)
Wednesday, March 16, 2011 3:46 PM
Hmm. I don't know the int vs. int32 discussion and I really dislike all search engines and of course: a link will never help me.
So please give me all details (including all flames of course!) while I go and get me a pepsi and popcorn :)
SCNR
Konrad
Wednesday, March 16, 2011 4:03 PM
Not sure if I remember the "int vs Int32" discussion correctly... :-D
Do you dare me to go and bump it? ;)
Do it do it do it!!!!!
Btw, JohnGrove, are you off that emaillist meanwhile?
"The improbable we do, the impossible just takes a little longer." (Steven Parker)
Wednesday, March 16, 2011 4:34 PM
Hmm. I don't know the int vs. int32 discussion and I really dislike all search engines and of course: a link will never help me.
So please give me all details (including all flames of course!) while I go and get me a pepsi and popcorn :)
SCNR
Konrad
I think they refer to this one http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/014f9924-fc25-4f48-a9c5-b3e86566a24a/ the issue is very common, but more interesting is the fact that you dislike search engines and hyperlinks
Let us discuss that instead
Thursday, March 17, 2011 12:38 AM
I think they refer to this one http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/014f9924-fc25-4f48-a9c5-b3e86566a24a/ the issue is very common, but more interesting is the fact that you dislike search engines and hyperlinks
Let us discuss that instead
Dude! He was joking!
Or - maybe you are too!
Arrgh, I just don't know any more...
Thursday, March 17, 2011 12:49 AM | 1 vote
"And now for something completely different.", Monty Python
"Things that make you say, 'Hmm.' ", Arsenio Hall
Look! I can define my own custom alias name for System.Object. You just gotta love case sensitivity to fully appreciate this. Please retain the namespace definition that I used if and when you test the code snippet that appears just below. My snippet uses Reflection, and so it assumes that the namespace is "ObjectDemo". Call the method named "Test_A".
namespace ObjectDemo
{
using System.Reflection;
using OBJECT = System.Object; // define my custom alias name
public class ObjectTest
{
public static object obj1 = new object(); // use standard alias name
public static Object obj2 = new Object(); // use standard type name
public static OBJECT obj3 = new OBJECT(); // use my custom alias name
// in the lines just above, any right hand side could have been used with any left hand side.
public static void Test_A() // call this method to test the code
{
Type typeInfo = Type.GetType("ObjectDemo.ObjectTest", true, true); // Reflection is used here
WriteTypeInfo(typeInfo);
obj1 = "Polymorphism at Work";
obj2 = 3;
obj3 = new ObjectTest();
WriteTypeInfo(typeInfo);
return;
}
private static void WriteTypeInfo(Type typeInfo)
{
Console.WriteLine();
FieldInfo[] fieldInfos = typeInfo.GetFields();
foreach (FieldInfo fi in fieldInfos)
{
Console.WriteLine("The instance named {0} is type {1} = {2}.",
fi.Name,
fi.FieldType.ToString(),
fi.GetValue(fi).ToString());
}
}
public override string ToString()
{
return "Kilroy was here"; // you must be older than dirt in order to remember this guy.
}
}
}
Notice that the ToString method is called on the object's "Value". The results are different because the types that define the Value are different. Each of the types override the ToString method, which is defined in the base class, System.Object. This is Polymorphism at work. There are no differences between the 3 objects in the resulting IL.
Some would argue that is best to use the predefined alias name, "object", to allow for widest amount of cross platform compatibility. I would agree with that reason in this case. The keyword would allow your code to target other Frameworks besides the Microsoft .NET Framework, which might could a different type name for an equivalent type to System.Object.
Rudy =8^DMark the best replies as answers. "Fooling computers since 1971."
http://rudedog2.spaces.live.com/default.aspx
Thursday, March 17, 2011 12:50 AM
If that is the case then my programming style in which I am explicit will have saved me :-)
John Grove, MCC - Senior Software Engineer
You're "it". You have the last post on the int vs. int32 thread.Mark the best replies as answers. "Fooling computers since 1971."
http://rudedog2.spaces.live.com/default.aspx
Thursday, March 17, 2011 3:04 AM
Never draw fire; it irritates everyone around you. :p
Eyal, Regards.
Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.
SharpHighlighter - is an extension for Visual Studio, a fairly simple code highlighter for C#.
Thursday, March 17, 2011 3:09 AM
I think they refer to this one http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/014f9924-fc25-4f48-a9c5-b3e86566a24a/ the issue is very common, but more interesting is the fact that you dislike search engines and hyperlinks
Let us discuss that instead
I fear that eating popcorn and discuss something does not fit together. But I agree - such a topic could be a great ressource for flames. So I like that idea, too. :)
Sorry, if my joke wasn't 100% clear to understand. But after seeing an off topic discussion between 2 guys I was simply happy that the topic changed to the int vs. int32 discussion and I just wanted to contribute something for the fun, too.
Konrad
Thursday, March 17, 2011 6:58 AM
Rudy,
A pity that the object dog cannot walk while the cat can.
static void Main(string[] args){
Mammal cat = new Mammal();cat.Walk();}
object dog = new Mammal();dog.Walk();
}
class Mammal : Object
{public void Walk() {Console.WriteLine("Walk");}}
Success
Cor
Thursday, March 17, 2011 7:45 AM
It was clear, and I cracked a real life smile reading it...
The follow up from Serguey is just hilarious as well. :-D
"The improbable we do, the impossible just takes a little longer." (Steven Parker)
Thursday, March 17, 2011 7:46 AM
Rudy,
A pity that the object dog cannot walk while the cat can.
static void Main(string[] args){ Mammal cat = new Mammal();cat.Walk();} object dog = new Mammal();dog.Walk(); } class Mammal : Object {public void Walk() {Console.WriteLine("Walk");}}
Success
Cor
Lazy dog...
static void Main(string[] args){
var cat = new Mammal();cat.Walk();}
var dog = new Mammal();dog.Walk();
}
class Mammal : Object
{public void Walk() {Console.WriteLine("Walk");}}
"The improbable we do, the impossible just takes a little longer." (Steven Parker)
Thursday, March 17, 2011 9:10 AM
Lazy dog...
static void Main(string[] args){ var cat = new Mammal();cat.Walk();} var dog = new Mammal();dog.Walk(); } class Mammal : Object {public void Walk() {Console.WriteLine("Walk");}}
In that case the dog is anonymous and the object dog behaves like a Mammal. The same like this code bellow.
{
static void Main(string[] args) {
Mammal cat = new Mammal(); cat.Walk();
//object dog = new Mammal();dog.Walk(); Wont go.
object dog = new Mammal(); ((Mammal) dog).Walk();}
}
class Mammal : Object
{public void Walk() {Console.WriteLine("Walk");}}
}
Success
Cor
Thursday, March 17, 2011 9:35 AM
Hi,
I fear that this can be misunderstood easily:
"In that case the dog is anonymous and the object dog behaves like a Mammal."
Through the var keyword, a variable is not anonymous or something like that. The var keyword is only a "I am to lazy to type in the type so please compiler: Do it for me!". It is called implicitly typed. Please see http://msdn.microsoft.com/en-us/library/bb383973.aspx for details!
So the dog is a Mammal and not just a plain object.
But the other code you post is what I thought too. You create a Mammal (dog) and then hypnotize it and tell it "You are a dumb object and youc an only lie around". So of course: The dog will not walka round because it thinks it is a simple object that cannot walk. But if you tell him: "Come on, you are a Mammal!" the dog will believe you and walk. (But only for this short time. It forgets that immediately :) )
With kind regards,
Konrad
Thursday, March 17, 2011 9:47 AM
Hi,
I fear that this can be misunderstood easily:
"In that case the dog is anonymous and the object dog behaves like a Mammal."
Through the var keyword, a variable is not anonymous or something like that. The var keyword is only a "I am to lazy to type in the type so please compiler: Do it for me!". It is called implicitly typed. Please see http://msdn.microsoft.com/en-us/library/bb383973.aspx for details!
So the dog is a Mammal and not just a plain object.
But the other code you post is what I thought too. You create a Mammal (dog) and then hypnotize it and tell it "You are a dumb object and youc an only lie around". So of course: The dog will not walka round because it thinks it is a simple object that cannot walk. But if you tell him: "Come on, you are a Mammal!" the dog will believe you and walk. (But only for this short time. It forgets that immediately :) )
With kind regards,
Konrad
You're right about the var keyword, perhaps Cor had a slight mistake with the dynamic keyword, which is more like: "trust me, compiler, this'll probably work at runtime..."
Regarding the code... Actually, I think you only hypnotize yourself into thinking it's just an object, and it cannot walk... The dog always believes he can walk, it's just the caller that cannot see the truth...
"The improbable we do, the impossible just takes a little longer." (Steven Parker)
Thursday, March 17, 2011 9:52 AM
Regarding the code... Actually, I think you only hypnotize yourself into thinking it's just an object, and it cannot walk... The dog always believes he can walk, it's just the caller that cannot see the truth...
Ohh ... you are correct of course. I am really sorry, but when I tested this with my dog I simply thought: That is a stone now!
But I asked my wife and she told me: The dog was always a god! So it seems my try to hypnotize it was hypnotizing myself. I was really sure I has a simple stone in front of me and was happy that the stone was running when I told him: "Come one - you are a dog ... get that stick!".
Konrad
PS: Yes, this is also fun. Please do not believe, that I really hypnotized a dog ... I don't even have a dog ... :)
Thursday, March 17, 2011 11:02 AM
There is a real difference, but it's relevant only if you want it to be.
object is always the CLR-defined object type from which all classes derive.
System.Object can be whatever you define it to be. Nothing prevents you from creating a System.Object class.
Thursday, March 17, 2011 2:36 PM
Regarding the code... Actually, I think you only hypnotize yourself into thinking it's just an object, and it cannot walk... The dog always believes he can walk, it's just the caller that cannot see the truth...
Ohh ... you are correct of course. I am really sorry, but when I tested this with my dog I simply thought: That is a stone now!
But I asked my wife and she told me: The dog was always a god! So it seems my try to hypnotize it was hypnotizing myself. I was really sure I has a simple stone in front of me and was happy that the stone was running when I told him: "Come one - you are a dog ... get that stick!".
Konrad
PS: Yes, this is also fun. Please do not believe, that I really hypnotized a dog ... I don't even have a dog ... :)
Dammit, I read your PS too late...
I have a 5 month old boxer, and I swear to god he just barked "Class Bark... Cast... Woof... Exception"...
"The improbable we do, the impossible just takes a little longer." (Steven Parker)
Sunday, September 11, 2011 11:38 PM
Just to elaborate, there is a slight semantic difference between the two. Even though they act nearly identically, they are not the same type for overload resolution. For example, void f(params object [] myargs) {} void g(params Object [] myargs) {} object [] x = new object[5]; f(x); // passes myargs=x g(x); // passes myargs[0]=x Whether or not this counts as a "semantic" difference I'm sure will be hilariously debated ad nauseum, if the prior history of this thread is any indication.
If that were true, it would be a bug, since 'object' is defined by the language to be an alias for 'Object' (see http://msdn.microsoft.com/en-us/library/9kkx3h4c%28v=VS.100%29.aspx).
However, it isn't actually true as this program demonstrates:
using System;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
object [] x = new object[5];
X = x;
f(x); // passes myargs=x
g(x); // passes myargs[0]=x
}
private static object X;
static void f(params object [] myargs)
{
if (myargs == X)
{
Console.WriteLine("In f(), myargs == x");
}
if (myargs[0] == X)
{
Console.WriteLine("In f(), myargs[0] == x");
}
}
static void g(params Object [] myargs)
{
if (myargs == X)
{
Console.WriteLine("In g(), myargs == X");
}
if (myargs[0] == X)
{
Console.WriteLine("In g(), myargs[0] == X");
}
}
}
}
The output of this program is:
In f(), myargs == X
In g(), myargs == X
This shows that the same item (namely 'x', the array of objects) is passed to both functions.
If you inspect the IL for that program, you'll see that identical calling code is generated.
Reflector shows the IL code is generated like this:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
.maxstack 1
.locals init (
[0] object[] x)
L_0000: ldc.i4.5
L_0001: newarr object
L_0006: stloc.0
L_0007: ldloc.0
L_0008: stsfld object Demo.Program::X
L_000d: ldloc.0
L_000e: call void Demo.Program::f(object[])
L_0013: ldloc.0
L_0014: call void Demo.Program::g(object[])
L_0019: ret
}
Note that the code for the two calls is identical (as it has to be).