Would I be telling the truth if I was to say that a type in C# is defined by a class. An example of this is the String class. And with this class, I can make instances of it, for example, a simple variable declaration:

string myName = "Usmaan";

The string variable myName is now referred to as an object.

Am I right to say those statements?

Kindly reply with a Yes or No before you give your views. Sometimes it's important not to jump the gun by blasting amazing definitions of certain functionalities...Especially when it comes to a new programmer like myself.

Your contribution to the matter is extremely appreciated.

UsSy

Recommended Answers

All 3 Replies

Yes you are correct, however string is a bit of a special case in the way that it is initialised. A better description of objects is to look at your post in your other thread where you had:

Human human = new Human();

In this example, you are creating an object of type Human. Inside the Human class there will be a constructor - this is a special method that allows you to create an instance of the object of type Human.

The reason why I say that string is a special case is that yes, string is an object, however it allows a shortcut in the way you create an instance of it - you don't have to use "new", you can just set its value as though it were a primitive type (lowest level data type in a language which stores a value but is not actually an object).

Ahah, I see. That makes good sense.

So to re-iterate what you said for my and for other peoples benefit.

The reason why you said String is not the best of all examples is due to the fact that it is a low-level data-type. The compiler has the class String as a built in function/type so there is no need to specify/go through the trouble of calling a constructor of that class as to how the

Human human = new Human();

Code is implemented.

Nevertheless, human is now an object of type Human and this is a reference type. This particular object is sitting on the Heap, not on the stack.

Is that correct?

Kindly reply with a Yes or No before you give an explanation.

Thank you for your generosity, kindness and help.

UsSy

Sorry no. String is also an object, but to use it in your example of how to create objects is misleading, because C# allows you to take a shortcut as though it were a primitive type.

Human human = new Human(); // creates an object
int daysToNextBirthday = 43; // creates a primitive by assigning a value directly, no call to constructor
string monthOfBirth = "February"; // looks like a call to a primitive but it is just a shortcut, it actually creates an object for you - String is the only class I can think of that does this
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.