VENTAS: 1-800-867-1389

 none
Anyone please explain Object Type in C#

    Pregunta

  • Can someone please explain Object Type in C#?

    I was reading a C# Tutorial on http://www.tutorialspoint.com/csharp/csharp_data_types.htm where I was reading about Object Type which is inturn a Reference Type.

    I have also gone though the following MSDN link:

    http://msdn.microsoft.com/en-us/library/9kkx3h3c%28v=vs.80%29.aspx

    Here, it is written : "The object type is an alias for Object in the .NET Framework. In the unified type system of C#, all types, predefined and user-defined, reference types and value types, inherit directly or indirectly from Object. You can assign values of any type to variables of type object."

    On other site "http://www.dotnetpearls.com/object":, it is written: "Object is the base class for all derived classes. It provides some methods and capabilities."

    Now, I am confused. I only know of Object as the instance of any Class so I am unable to understand object in the above context and also, please guide as to why/ in which simple situations we use object type?


    http://learn2automate.wordpress.com Vijay Gupta


    • Editado gupta vijay domingo, 07 de julio de 2013 6:56 provided more information
    domingo, 07 de julio de 2013 6:52

Respuestas

  •  

    Hi Vijay,

    Object is an instance of a class. Every Object in .Net framework is inherited from Object class. This inheritance is created to reuse certain feature of the base class which is very much required by all the derived classes.

    Object will hold all the instance data members and member functions of a class. Objects are used to communicate between the classes.

    Ex: Consider a class Employee as below which has two data members such as "Name" and "Salary".

    public class Employee

    {   

    string Name;

    int salary;

    }

     

    The Employee class by default inherited from Object class and avail the default methods from the Object class such as "Equals" , "GetType" ,GetHashCode and ToString().

    As Employee class inherited from the Object class you can directly convert the Employee class object into Object class.

    Object obj = new Employee(); This is called up casting which is allowed in .Net framework.

    Hope this explanation helps to clear you question.

    domingo, 07 de julio de 2013 7:11
  • Gupta, your points 1 and 2 are correct except that they are explanations of the term 'object' and not explanations of 'class' as your preceding sentence said.

    It is unfortunate when technical terms have multiple meanings.  As a purely personal preference I attempt to avoid using the word object in your second sentence.  I prefer to talk about 'instances of a class' rather than 'objects of a class' just to avoid the confusion of meaning.  The C# standard says

    "A class provides a definition for dynamically created instances of the class, also known as objects."

    so, the meaning of 'object' as being equivalent to 'instance of a class' is defined in the standard (section 1.6).

    System.Object is defined by the .Net framework and is the root of the inheritance hierarchy.  In c# an alias called 'object' is provided for this .Net class.  The C# standard says

    "Omitting a base class specification is the same as deriving from type object" (section 1.6.4)

    This can be stated differently (but equivalently) as "The .Net type called System.Object is the ultimate root of all classes".

    I think you meant your third point to be

    3) UNlike C#, there is no Object base/root class in C++.

    I have forgotten more C++ than I remember, so I can't comment as to whether there is an ultimate root of all classes in C++.  But there certainly is in C# - and it is called 'object'.


    Paul Linton

    domingo, 07 de julio de 2013 9:59
  • The type of object has one advantage - Everything can go in such a varriable:
    All classes directly or indirectly inherit from object, so Inheritance allows them to fit.
    And all Structs/value type can be boxed to fit into object.

    On the other hand, in most cases you should go the way of using generics when you write something that can apply "to any class or structure".

    Object has one big drawback over generics: It is not typesafe. As I said, everything can go in there.
    So you never know for certain what will be in one. Stuff like the sender of an event are customary objects. But for most other cases, avoid object and use generics.


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2

    domingo, 07 de julio de 2013 15:57

Todas las respuestas

  •  

    Hi Vijay,

    Object is an instance of a class. Every Object in .Net framework is inherited from Object class. This inheritance is created to reuse certain feature of the base class which is very much required by all the derived classes.

    Object will hold all the instance data members and member functions of a class. Objects are used to communicate between the classes.

    Ex: Consider a class Employee as below which has two data members such as "Name" and "Salary".

    public class Employee

    {   

    string Name;

    int salary;

    }

     

    The Employee class by default inherited from Object class and avail the default methods from the Object class such as "Equals" , "GetType" ,GetHashCode and ToString().

    As Employee class inherited from the Object class you can directly convert the Employee class object into Object class.

    Object obj = new Employee(); This is called up casting which is allowed in .Net framework.

    Hope this explanation helps to clear you question.

    domingo, 07 de julio de 2013 7:11
  • Thanks Shailedra for the reply.

    I know object concept and as I know it, it is the instance of any class (I know C++).

    Now, as it seems from your explanation and what I read that there are 2 explanations to class in C#:

    1) "Object is the name of the class which is root/base class in C# from which all the other classes, data types etc are derived by default (implicitely as you also did not inherited your Employee class from any class)

    2) Object is the instance of any class. Suppose if you say Employee emp, then emp is the object of the class Employee

    3) Like C#, there is no Object base/root class in C++

    Am I correct in above 3 points?


    http://learn2automate.wordpress.com Vijay Gupta

    domingo, 07 de julio de 2013 7:39
  • No!!!  A Class is an object definition just like string, int, float.  A class is different from other definitions the it can also includes executable statements.

    An object is an instance of a class.  An object is a pointer to memory space a must be real.  Not a definition.  So an integer type is 4 bytes of memory.   

    So if we say : int a = 5

    The object 'a' is located at 0x4444455 in memory and the contents is 5.  The object isn't 'int' an integer.


    jdweng

    domingo, 07 de julio de 2013 8:52
  • @Joel - I know what you are saying but how it relates to my query, I am not able to understand. Can you please elaborate?

    @Shailendra/anyone - Can someone please respond to my previous post in this thread?


    http://learn2automate.wordpress.com Vijay Gupta

    domingo, 07 de julio de 2013 9:05
  • Greetings Gupta.

    If you know c++, then you will be aware that there is a class called CObject, from which many other classes are derived.

    Well, in c# there is a class called object from which all other classes are derived. Even if you create your own class and don't explicitly derive it from any other class, the compiler will derive it from object anyway.

    The class called "object" (lower case "o") is an alias for the .NET framework class "Object" (capital "O"), so object and Object are the same class. 
    domingo, 07 de julio de 2013 9:27
  • Hello,

    System.Object (Object) is the type that the .NET framework implements and is the base type for all .NET types whereas object is a keyword of the language (in this case C#), under the hood the compiler changes it to System.Object so it's merely a convenience, they are interchangeable and it's really a matter of style.

    Some people prefer to use the type directly to make the code more consistent whereas others prefer to use the language keyword to distinguish native types from their own custom types.

    One thing to note here and a subtle difference yet might be important is that the language keyword is equivalent to the full name, meaning the namespace + the type name.


    Regards,

    Eyal Shilony

    domingo, 07 de julio de 2013 9:30
  • Gupta, your points 1 and 2 are correct except that they are explanations of the term 'object' and not explanations of 'class' as your preceding sentence said.

    It is unfortunate when technical terms have multiple meanings.  As a purely personal preference I attempt to avoid using the word object in your second sentence.  I prefer to talk about 'instances of a class' rather than 'objects of a class' just to avoid the confusion of meaning.  The C# standard says

    "A class provides a definition for dynamically created instances of the class, also known as objects."

    so, the meaning of 'object' as being equivalent to 'instance of a class' is defined in the standard (section 1.6).

    System.Object is defined by the .Net framework and is the root of the inheritance hierarchy.  In c# an alias called 'object' is provided for this .Net class.  The C# standard says

    "Omitting a base class specification is the same as deriving from type object" (section 1.6.4)

    This can be stated differently (but equivalently) as "The .Net type called System.Object is the ultimate root of all classes".

    I think you meant your third point to be

    3) UNlike C#, there is no Object base/root class in C++.

    I have forgotten more C++ than I remember, so I can't comment as to whether there is an ultimate root of all classes in C++.  But there certainly is in C# - and it is called 'object'.


    Paul Linton

    domingo, 07 de julio de 2013 9:59
  • Thanks all for the reply.

    @Paul....you have given the exact replay what I was asking.

    Now, I just want to ask as to why/ in which simple situations we use object type and what is the benefit of using it over any other type?


    http://learn2automate.wordpress.com Vijay Gupta

    domingo, 07 de julio de 2013 14:23
  • The type of object has one advantage - Everything can go in such a varriable:
    All classes directly or indirectly inherit from object, so Inheritance allows them to fit.
    And all Structs/value type can be boxed to fit into object.

    On the other hand, in most cases you should go the way of using generics when you write something that can apply "to any class or structure".

    Object has one big drawback over generics: It is not typesafe. As I said, everything can go in there.
    So you never know for certain what will be in one. Stuff like the sender of an event are customary objects. But for most other cases, avoid object and use generics.


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2

    domingo, 07 de julio de 2013 15:57
  • @Chris - Now, whats the difference between object and generics?

    I thought object and generics are same because any type can go to object type.


    http://learn2automate.wordpress.com Vijay Gupta

    domingo, 07 de julio de 2013 17:25
  • very simply,

    object is merely a name given to the primary class
    of .net. from which all other Types(classes, etc..) are derived by default. generics is a system provided on .net by which the compiler of a language produces redundant code for you. which otherwise you would have to type for all the various Object Types(classes etc..) for a piece of code which works and looks similar execpt if we were to change Types in that code.

    generics does what Templates do in c++. but in much constrained and reduced way. i.e not all the things that you can do with templates can be done with generics

    c++ developed over C and C didnot have classes whereas .net was designed as a object oriented framework to which a primary base class was provided. that might explain to your observation that c++ does not have Primary base class. because it is not a framework but a language where as .net is a framework. on which various languages work.

    althou technically i maybe wrong it is a laymans
    statement.

    Thanks

    Regards


    • Editado RIGHT_THEN domingo, 07 de julio de 2013 18:31 forgot about c++
    domingo, 07 de julio de 2013 18:13
  • @Chris - Now, whats the difference between object and generics?

    I thought object and generics are same because any type can go to object type.


    http://learn2automate.wordpress.com Vijay Gupta

    No, they are quite differently.

    The good thing with object is, everything can go in.
    The bad thing with object is, everything can go in.

    Generics in turn allow you to define a "Type placeholder".

    Let's compare List<object> and List<Person> and List<Places> (with Places and Person being classes you wrote).

    The designer of List<T> could just have written a List of objects and avoided all the extra work of generics. You could certainly put everything in, but it would not be typesafe: Every time you use anything that is in there, you have to cast and there is a chance that someone put a Person Instance into your ListOfPlaces. Perhaps even you by accident. Likely in a totally different function. Perhaps even outside of your current project, within code you did not looked at for 5 years.

    Now List<Person> is a list into wich only Person Instances can go. The same way you cannot write "Person someGuy = new Place("Honkong")", no programmer can by accident (or intent) assign anything but a person to this list. It only takes Persons. It only Stores Persons. It only gives out Persons.

    What you store in it is not relevant for a list. The concept of a self-growing Collection is thie same, wheter you store Persons, Places or Integers. But still typesafety is an very important issue.


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2

    domingo, 07 de julio de 2013 18:58
  • c++ developed over C and C didnot have classes whereas .net was designed as a object oriented framework to which a primary base class was provided. that might explain to your observation that c++ does not have Primary base class. because it is not a framework but a language where as .net is a framework. on which various languages work.

    Just to clarify, while C does not have a class construct it is completely possible to write object-oriented software with it.

    Object-oriented is first an approach to develop software which is based on object-oriented principles, engineers around the globe started to write object-oriented languages that take some of the principles and various best practices and implemented them into the language and made some of them a first-class citizen.

    C++ does not have an object root not because it lacks a framework but because it was a design choice, however, C++ has a framework and a run-time. :)


    Regards,

    Eyal Shilony


    lunes, 08 de julio de 2013 3:37
  • It's fun time reading the important C# coders like you and el-shitlony (ROFL).

    I know you suffer from dyslexia but my name is Eyal Shilony not el-shitlony so use your glasses and your little brain and write the letters in the correct order.

    Are you having the parrot syndrome? you can write ROFL once and people will probably understand it, you don't need to write it 10 times in a single post.

    Here is an example "I just ROFLMAO at your poor knowledge, English and education."

    It's good that you can actually read posts in this forum because most of us can barely read your posts. do us all a favor and read the C# specification 10 thousand of times more so you can finally write some proper English. :)


    Regards,

    Eyal Shilony


    lunes, 08 de julio de 2013 13:14
  • I already told you that many times before but I'll tell you once again, I really don't care about anything you write or think or do, you're yet another atom in my life that I don't need to see and don't want to see. :)

    Hopefully, the coin will drop now and you will realise that you can save your curses and whatever that you want to prove to the rest of the world to yourself!


    Regards,

    Eyal Shilony

    lunes, 08 de julio de 2013 16:25
  • You do... and always reply with some insignificant post.

    I reply to your posts not because I care but because you're amusing.

    Not to mention that you can ignore these insignificant posts and instead criticize how bad people are (because you apparently awesome...) you can actually write something useful.

    Much ingratitude. I've taught you so many things since you began to reply to me,... writing all those C# misconceptions...

    See, when I wrote amusing above that was exactly my point.

    P.S. I really think that your habit to copy/paste bunch of old quotes from god knows how many posts doesn't contribute to anything but wasting storage.


    Regards,

    Eyal Shilony


    lunes, 08 de julio de 2013 17:34
  • Eyal,

    You give this person too much credit by even responding to the garbage he posted.  Ignoring it is the best way to get your "revenge."

    I find your contributions on this forum quite informative and helpful - as well as clearly worded.  Do not be dismayed by the trollers!

    BitWise


    BitWise_

    martes, 09 de julio de 2013 0:21
  • @BitWise_, you're right.

    Thank you for your kind words. :)


    Regards,

    Eyal Shilony

    martes, 09 de julio de 2013 2:26
  • Yeah, a name like "yourefunnymakemeROFL" and multiquoting (how do you even do that in this forum?) - especially on a per setence level - pretty dead giveaway for being a troll.


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2

    martes, 09 de julio de 2013 8:15
  • multiquoting (how do you even do that in this forum?)

    Well, you can't do that using the Rich-Text Editor but you can click on the [HTML] button to open the HTML editor and insert the quotation markup like this <blockquote>Another person's quote</blockquote>.

    I hope it helps.


    Regards,

    Eyal Shilony


    martes, 09 de julio de 2013 8:26
  • Maybe a more important question is, why are the garbage posts STILL not deleted from this thread, days after they were reported as abusive?

    Can we hear from one of our administrators on this - I realize you good people are (busy) volunteers, but is there anything that can be done to remove the posts?


    BitWise_

    miércoles, 10 de julio de 2013 17:10
  • Just ignore them. Trolls usually starve to dead fast around here.

    And the posts are barely up for 2 days, cut the mods soem slack.


    Let&#39;s talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    miércoles, 10 de julio de 2013 18:18
  • And the posts are barely up for 2 days, cut the mods soem slack

    Hmm, well, I for one grateful to have moderators but if they aren't up for the task or aren't active enough they shouldn't moderate this forum.


    Regards,

    Eyal Shilony

    miércoles, 10 de julio de 2013 18:32
  • Just ignore them. Trolls usually starve to dead fast around here.

    And the posts are barely up for 2 days, cut the mods soem slack.


    Christopher84,

    I am not criticizing the admins/moderators - actually, I acknowledged that they are busy, and volunteering their time.  I am not blaming them, I simply want to hear from them on this, if there is a reason why the posts can't be deleted.

    In this case, however, there is both profanity and an intentional act of disrespect toward another member.  While ignoring the troller is the recommended approach (and one I pointed out already), that's more easily said than done with a personal attack - the sooner the junk is removed, the better for all, IMHO.  We risk losing the insight of knowledgeable folks on these forums when that kind of stuff is not dealt with quickly.  People see that and figure, the heck with it - why should I put up with that in order to help someone else (for free!)?


    BitWise_

    miércoles, 10 de julio de 2013 20:45
  • Hmm, well, I for one grateful to have moderators but if they aren't up for the task or aren't active enough they shouldn't moderate this forum.

    The thing with things provided free of charge - everyone things he can do it better, but nobody wants to actually do it.

    If you really think this forums needs more moderator activity, you could always become one yourself :)
    I already do a job more invovled than this in my freetime, so I'll pass.

    A good reason why it might not be possible: Other post that followed (belong to the same tree as hsi post) would perhaps be deleted as well.


    Let&#39;s talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    miércoles, 10 de julio de 2013 21:14
  • @bichwise__

    You give this person too much credit by even responding to the garbage he posted.  Ignoring it is the best way to get your "revenge."

    I find your contributions on this forum quite informative and helpful - as well as clearly worded.  Do not be dismayed by the trollers!


    Garbage is what your el-shitlony friend wrote in Struct or class for coordinates : The C# Specification is where the error comes from... and I now  understand why it is also stated in a popular C# book.

    This is sheer stupid arrogance from a el-shitlony that just do IntelliSense code.

    In this current thread, el-shitlony wrote that System.Object  is ... the base type for all .NET types, but wrote the opposite in an older thread with much arrogance, with the help of a knave and thief mvp that stole my account to defraud a thread,as if I participated in it.

    All those garbage is not perceived by you; the smell of it is common place for you.

    Probably, you're merged into it.

    If moderators delete all garbage, you run the risk of being collected, deleted and trashed as well .

    There are so many examples of stupid C# misconceptions that I forgot to mention one from your another friend: Object has one big drawback over generics: It is not typesafe.

    ROFL.

    And you didn't say a single word against such absurd!

    Probably, you don't see anything wrong with that assertion.

    ROFL even more.

    jueves, 11 de julio de 2013 0:16
  • If you really think this forums needs more moderator activity, you could always become one yourself :)
    I asked them about it but never got a reply, so I assume they pick people that they know or active within Microsoft.

    Regards,

    Eyal Shilony

    jueves, 11 de julio de 2013 5:06
  • So far I found that for the pure amount of posting there is a surpsingly low ammount of Trolls. As far as I can tell ROFL is the first troll I ever saw on this Forum. And I have almsot 1300 posts subscribed.
    It almost seems as if they are scared away from programmers and other IT people because they know they can't compete. Or perhaps they are just more active in other Forums?

    The spambots seem a much greater issue, despite the progress in fighting them.

    But I guess we should stop using this thread for off topic discussions.


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    jueves, 11 de julio de 2013 8:15