C#/CLASSES AND OBJECTS
Everything in C# associated with classes and objects.
Along with its properties methods and constructors and many more things.
This concept is came in C# from real life.
For example in real life car is object and its attributes are weight,color and method such as drive and brake.
Classes and objects are fundamental concept in C# to concise our code and for better user experience.
A class is a template for creating objects.
CREATE A CLASS
To create a class use class keyword with class name.here is a picture to show how to create a class.
It is not mandatory but its good practice to write first letter in uppercase of name of class.
when we declare any variable in class it becomes field(or attribute) of class.
CREATE A OBJECT
We have above seen how to create a class.Now we see how to create objects of class.
To create an object of Car, specify the class name, followed by the object name, and use the keyword new:and after again class name with paranthesis ().
Here class name is car. we have created object named myobj of class car with new keyword.
MULTIPLE OBJECTS
We can create multiple objects in a single class but with different object name.
Here we can see two objects of class are cretaed with different names.
If we want to access variables/fields inside a class we use(.)symbol as we can see in above picture we accessed variable with object name (myobj.color).
CLASS MEMBERS
Fields and methods inside a class are known as class members.
We can create multiple fields and methods in a single class.
Here we create two fields with named color and maxSpeed and one method with named (public void fullThrottle()).
We can also leave the fields blank and add value later when we create object .
OBJECT FIELDS/METHODS
variables inside a class called fields and it access with (.)keyword with object name as we seen above.
Methods are used to perform certain actions.
Methods belongs to class and they define how the objects of a class behave.
just like fields we can access method with (.)syntax and remember that we use the name of the method followed by two parentheses () and a semicolon ; to call (execute) the method:
MULTIPLE CLASSES
We can also use this way that we create fileds and methods in one class and acess them through another class.
This process is use for better organization of our code that one class conatin all fields and methods while in our Main class we only access them.
In Summary we have seen what is class and objects and how to create them,we also seen concept of multiple objects ,fields and mthods and mutiple classes.
classes define the structure and behavior, while objects are instances of classes that hold specific data and can perform actions defined by the class.Classes and objects are fundamental concepts in C#.
Take the first step towards data-led growth by partnering with MSA Infotech. Whether you seek tailored solutions or expert consultation, we are here to help you harness the power of data for your business. Contact us today and let’s embark on this transformative data adventure together. Get a free consultation today!
We utilize data to transform ourselves, our clients, and the world.
Partnership with leading data platforms and certified talents