1
Crost
6y

Any C# Devs around that could tell me what the difference between Type and object is please? I'm really stuck for googling it.

Comments
  • 0
    Isn't type refering to an actual object-type? So for example

    class Product

    Defines the "Product"-Type

    Dont know this for sure ;)
  • 2
    In c# object is actually a type as well, it's the base type for all primitives.

    But then there Type as well...?
  • 1
    In OOP the object refers to the specific instance of a type.

    So for example in Java.

    Person p = new Person();

    p is the instance of Person.

    In other words. p is the object. Person is the type.

    We could have multiple person objects.

    Person p1 = new Person();
    Person p2 = new Person();

    Both p1 and p2 are of the Type Person.

    But they are not the same instance.
  • 1
    @BigBoo in c# object is also a type that all primitives inherit from.

    I think the type 'Type' is a classification in that every type is a type but primitives don't inherit from it, they are Types.

    Unsure.
  • 1
    @SanitizedOutput there is a strong possibility I'm wrong
  • 0
    @craig939393 Both in C# and Java there is a class called Object aswell. So yes. An object can be of the type Object.

    However if you don't make distinctions between the type Object and the concept of objects. You are just going to get confused.

    It all makes sense when you get fluent with inheritance and interfaces. But it's not going to hinder you a lot as a developer if you don't get it at first.
  • 0
    @BigBoo so do you think I'm correct in what the class 'Type' is?
  • 0
    @craig939393 I'm not sure about your wording or what you mean.

    But in C# primitives are Objects aswell.

    But in Java we have boxed types which are immutable objects.

    For example Integer is the boxed type of int.

    Integer can be instanced into an object.
    int can not be instanced into an object.

    I hope I'm answering your question lol.
  • 0
    @BigBoo yeah I'm not really talking about object instantiation, but I appreciate your answer anyway.

    Cheers
  • 0
    @craig939393
    It's all semantics, and you probably don't have to sweat it if you don't get it directly. Just let it sit and think about it for a while. Maybe you will get an AHAAAA moment later lol 😀
  • 2
    object is a type (note the lowercase)
    Type (note the uppercase) is a type.

    In a variable with a Type type you can store types like this:
    var someVar = 1;
    Type varType = typeof(someVar);

    Could be wrong with the type retrieval of an object because I normally dont do it like this (but easier for explanations)

    What can this be used for? Lets say you have a variable of type IAnimal. This variable can store things that implement IAnimal. For example an instance of a Dog or Cat class.

    Using the Type you can differentiate the two things later on in your code.
  • 0
    @jschmold to be honest your explanation is less clear.

    type is not a class, Type is.
    But Type and int are both types
  • 0
    @jschmold you are saying the same thing but im explaining the difference between Type and type where you just mix the two again to make it less clear...
  • 0
    @jschmold just what I say: var b = 2

    Then the type of b is int.
    And with Type im really referencing the class.
  • 1
    @jschmold I just lost the concept of types in C#.
  • 0
    @jAsE-Official then you may not be a C# type of person
  • 0
    @Codex404 I just knew types...

    Before.
  • 2
    @jAsE-Official the explanation about types is more difficult than using it.
  • 0
    @Codex404 Yes, I think it is.
Add Comment