You are on page 1of 2

:Creating an object of a class:

You can say that you are going to create an “object of a class”. Or you can also say you are going to
create an instance, an “instance of an object”. An “Instance” is an Object. Now I usually say that “I’m
going to create an object of a data type, because everything in the .NET Framework is a Data Type. But
not everything in the .NET Framework is a class, there is a key distinction, the characters, the Booleans,
the integers, all of those are Data Types but they are also Classes.

But then we have a “String” which is a Data Type and also a Class. Data Types is a broad way of referring
to everything in the .NET Framework and the word class is just a certain type of a data type.

:Creating a String-Builder:
A String-Builder, it builds a string. We use a String-Builder to create many pieces that we want to create
into a string. Concatenating is inefficient because we need to create a larger portion of strings. So this is
where the String-Builder comes in, so that we can append different pieces of data and then build the
string through the String-Builder Class, and it’s more efficient than concatenation.

We want to create an Object and we want to create a reference that we can refer to it anytime we need
to. We could and can create an object and not assign to it a variable, but in doing so we guarantee that
we only want to use it once, because we don’t have a way to reference that object after it is created. But
here we want to create an object and assign to it a variable, so we can reference to it anytime we need
to, or re-use it anytime we create a code.

This is the data type name: StringBuilder

And this is the name of the variable: sb

And this is the common assignment symbol : =

We now will use an operator called “new” because we want to create a new string-builder object.

So here is the next line: new

Now we will call a “constructor” of the StringBuilder class. So the constructor is the same name
as the data type, which is “StringBuilder”.

Calling the new constructor: StringBuilder

Note: A constructor is like a special method, we do not have to have an object to be able to call the
constructor, which is why its special and not a method.

And to call the constructor we use the () and then we end the statement with a semicolon ;
So this is how the code will look like, this is now called a String-Builder object:

StringBuilder sb = new StringBuilder();

We can use this same pattern to create just about any other type
of object. For example, there is a datatype called DateTime,
which represents DateTime, it’s not a class, but it’s called a
struct. But we can create a datetime object in the same way we
created the String-Builder object, by using the new operator,
then calling the constructor, which is DateTime.
DateTime dt = new DateTime();

Note: This shows you that we can use the same pattern over and over again
to create objects. So there we have it, that’s how we create objects in C#.

You might also like