Class
A class represents a blueprint of an object.
- Currently, we know some basic data types:
- For representing numbers, we use types like
int
,long
,byte
,decimal
, etc. - For representing words, we use
string
or arrays of characters (char[]
). - For storing collections of numbers, we use arrays (
int[]
,float[]
, etc.).
- For representing numbers, we use types like
However, what about storing complex objects like Person, Planet, etc.?
- For example, a Person object might have properties like
Name
(a string),Age
(an integer), and methods likeIsCoding()
.
From the diagram above, we see that a class named Person
encapsulates a contextual representation driven by developers. It defines properties and behaviors specific to a person, allowing us to model and work with such complex entities in code.
Requirement
To proceed with this, you’ll need Visual Studio Community Edition for easier coding with auto-suggestion. Let’s create a brand new console project in Visual Studio.
Understanding Class Syntax in C#
In C#, we represent a Person using the class
keyword, encapsulating related properties and methods inside the class body. Below is a basic representation of a Person in terms of a class:
|
|
Now, let me explain each line step by step:
- public: This keyword in C# determines the accessibility level of something (class, field, or method). Here, it allows the
Person
class,Name
field,From
field, andIsWalking
method to be accessible from other classes. - We’ll delve deeper into programming concepts as you gain more confidence, or you’ll learn about them automatically as we progress.
Now we know that what class is and how to represent real thing in form of class
Question Now is How to use this class ?
In C#, we create an instance or object of a class and then utilize it. Let’s clarify what these terms mean:
Instance/Object: Imagine you’re an artist.
- Your task is to download the image below:
Once downloaded, your next task is to fill the CFD (Codefrydev 😁) with any color your artistic mind suggests.
Here’s how my programmer mind sees it:
Downloading the image is akin to creating an instance or object.
You can then manipulate the downloaded image as you wish, which won’t affect the version available on the website.
Filling the color is like assigning different values to the fields or properties of an instance in programming.
Create Instance Syntax
- To create an instance of the
Person
class in C#, follow the syntax:
|
|
Here, myself
is an instance of the Person
class, possessing all the characteristics defined within the class by default.
Visual Representation
- The
Name
field isnull
because it hasn’t been assigned a value in the class definition. - The
From
field has the value"Earth"
because it is initialized to this value in the class.
Now we know the syntax of how to create instance of class
Question now comes in mind that how do we modify ?
If you type myself.
and use IntelliSense in Visual Studio, you will see options related to the fields and methods defined in the Person
class. Here’s how you can modify the code to assign values to the object’s fields:
|
|
- you will have object with modified value as per above code.
- you can view in Visual Studio by using below code and pressing 5 key
|
|
- it will output somting like this
- using Terminal Command
|
|
Modification in class
changes in the class method IsWalking
|
|
- Calling a method in C# involves using the instance name followed by a period (
.
), the method name, opening and closing parentheses()
, and ending with a semicolon;
. This syntax executes the behavior defined within the method.
You can call this method on an instance of the Person
class like this:
|
|
- Source code Upto this point
- you can use new c# syntax Person myself = new(); instead of Person myself = new Person();
|
|
- now Run the application an you will have following.
Another Object
- Create another instance of the class
Person
and observe its default values. - Guess if the value will be the same as
myself
or something else. - Let’s solve this mystery by creating another instance of the class.
|
|
Surprisingly, its value is not the same as
myself
. Wait, what? Why?Okay, let me explain using the analogy of downloading an image.
If creating an instance is like downloading the image again,
Obviously, no matter how many times you modify or paint it with different colors, it’s still the same downloaded image.
A similar concept applies to modifying object instances as well.
Now, modify the object value of
friend
with a different value and see what happens.
|
|
- Now this data is held by a different object.
- Using the terminal:
- Source code upto this point
|
|
More about Object/Instance
Coming back to the analogy of downloading an image on your PC:
- Whenever you download an image, it will be stored somewhere on your PC.
- Each image will have a unique name in its specific folder.
- This means no two images can have the same name in the same folder.
- Each image will take up some space.
- Each downloaded image will have separate properties from other images.
- See the image below for demonstration:
- another image
- Similar to this, each instance of a class will:
- Have a separate location in memory where it is stored.
- Occupy a specific amount of space on your PC.
- Cannot have the same name as another instance.
- A folder is analogous to a namespace in C#.
For more information about classes, please refer to the Official documentation.