Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are objects a variable type or a name given to variables inside a class?

Tags:

variables

c#

oop

I've got a very basic understanding of OOP but I am a little confused as to what constitutes an object so sorry if this sounds like a stupid question.

public class StatMetaData
{
    public string key { get; set; }
    public string name { get; set; }
    public string categoryKey { get; set; }
    public string categoryName { get; set; }
    public bool isReversed { get; set; }
}

In my code i'm using this class to store parsed JSON information and I understand how it all works but was wondering if because these variables are part of their own class do these count as objects? Or does it have to have the object data type to be classed as an object?

If I'm totally off on my understanding any help would be much appreciated, websites I've looked at don't give a very good definition of what actually can be classed as an object.

like image 221
Obmama Avatar asked Sep 08 '25 00:09

Obmama


2 Answers

In C#, an object is a structured chunk of memory in the heap. The structure is defined by the class the object is an instance of. Instances of the classes object, string and your StatMeataData are all objects.

Variables, fields and properties are not themselves objects, but can store a reference to one.

like image 127
ILMTitan Avatar answered Sep 09 '25 13:09

ILMTitan


In c# almost everything is an object, let's take your code for example:

public class StatMetaData
{
    public string key { get; set; }
    public string name { get; set; }
    public string categoryKey { get; set; }
    public string categoryName { get; set; }
    public bool isReversed { get; set; }
}

all the items below are true:

object c = new StatMetaData(); //thus the instance of the class is an object
object k = new StatMetaData().Key; //an instance of string is also an object
object b = new StatMetaData().isReversed; //an instance of bool is also an object

In simple terms (and it goes beyond this simple statement) let's say an object is basically anything that you store in memory. When you declare a class of type StatMetaData you don't have an object because there's no instance, you're just declaring the Type... only when you create a new instance of StatMetaData you have an object in memory.

Also note that, for example object myObj; isn't an object in memory until you have something like object myObj = new object(); the object type is also that, a type.

like image 20
PedroC88 Avatar answered Sep 09 '25 14:09

PedroC88