This Tutorial Explain C# Classes and Objects. You will Learn About C# Class Members, Base Class, Methods, Parameters and Parameter Types with Examples:
Data Type Conversions in C# were explained in detail in our previous tutorial. In this tutorial, we will explore all about Classes and Objects in C# in detail.
Similar to most of the object-oriented programming languages C# also has inbuilt support for classes and objects.
=> Watch The Full C# Training Series Here
Table of Contents:
Classes And Objects In C#
An object in a programming language is similar to a real-world object. Object-Oriented Programming is a concept where the programs are designed using sets of classes and objects to simplify program development and maintenance.
A class is a logical collection of similar kinds of objects. It is one of the most fundamental types in C#. It is basically a data structure that is a combination of Methods, Functions, and Fields. It provides the definition for the dynamic instances i.e. objects that need to be created for the class.
For Example, if you are collecting data for a student in an application. There can be several properties of a student like a roll number, class, section, subject, etc. Each of these properties can be termed a property of student class and the student can be considered as a collection of all these properties.
So, here a class represents student i.e. the collection of objects and student attributes/properties can be termed as its objects. We will study these in detail in upcoming topics.
What Is A Class In C#?
Classes are created by using class declaration. A class declaration begins with the header that denotes attributes, modifiers, the name of the class, etc. The declaration is followed by the body of the class that contains member definitions between the curly braces “{“ and “}”.
A class can also have methods. A method defines the operations that can be performed inside a class.
Let’s create an operation to perform the simple summation of marks. To perform this we will create a main method inside the class.
What Did We Do In The Above Code Snippet?
We created a class of the name “Student”. Then we created some data variables with different values. We then created a “total_marks” integer variable where we stored the summation of marks of both subjects.
Object And Class Instance
Sometimes the terms class and object are used interchangeably but both are different entities. A class is the definition of the object but it’s not the object itself. The object is also known as an instance of the class. Class instances are created by using “new” operator. A new operator allocates memory for an instance and invokes a constructor to initialize it and returns a reference object.
For Example, if we want to create an instance for the class student then,
Student stu = new Student();
Here we have created an instance of class “Student” and we have defined “stu” as a reference object.
Class Members
Members of a class can be of static or instance type. Instance members are a part of the object whereas static members are a part of the class.
Let’s have a look at some of the class members:
- Fields: Variables present inside a class are called fields.
- Constants: Constant values present inside the class.
- Methods: Logical actions performed by the class.
- Constructors: Required to initialize the class or instance of the class.
What Is A Base Class?
The base class is a class that is used to create or derive another class. The class that is derived from a base class is called subclass or derived class. Although a base class doesn’t inherit any other class, the class that derives from a base class receives all data and behavior from the base class.
A base class may be specified by using the keyword class followed by class name and type parameters along with a colon followed by the base class.
Thus, in the above program, the derived class Student inherited the objects and the properties of the base class i.e. ClassName. As the derived class contains all members of a base class implicitly, we were able to print the “className” variable from the base class without creating any instance.
We will discuss more on inheritance in our upcoming tutorials.
Methods
Methods are the members of the class that implement a logical or computational action that is meant to be performed by the object or the class. Static methods if defined are accessed through the class and all the instance methods are accessed using an instance of the class.
Methods can also contain parameters that signify variable references being passed to the method. A method may also contain a return type that denotes the computational end product/value of the method that can be returned.
Some points to remember while working with the method are:
- If a method doesn’t return any value then the return types must be void.
- The signature or name of the method should be unique inside a class. The signature of a method means the name of the method along with the Parameters, Modifiers and Data type of the Parameters.
What Are Parameters?
Parameters are the values or reference variables that are passed to the method. Parameters receive their values from arguments that are specified while invoking a particular method.
Different Types Of Parameters Are:
- Reference Parameters
- Value Parameters
- Output Parameters
- Array Parameters
In most of our examples, we will be using either reference type or value type.
Let’s look at these in detail.
A reference parameter is used when you want to pass an argument via reference. This means that the argument pass as a parameter to the method should be a variable with a value and during execution, it should represent the storage location of the value of the variable.
The value parameter is used to pass the input values to an argument. The value parameter refers to a local variable that is passed as an initial value to the argument that is then passed as the parameter.
Given below is an Example to understand Class, Object, Methods, and Parameters better.
What Did We Do In The Above Code Snippet?
In the above code snippet, we have defined two-class i.e. “Marks” and “Student”. First-class Marks contain the method “marksRecieved”. In this method, we have passed “Reference Parameters” denoted by keyword “ref”. In this method, we have performed the simple summation of two marks obtained and then printed the result on the console.
Next, we have a class Student that contains the main method from which we will be calling the method from the Marks class. Before we make a call to the method we need to create an instance of the class Marks.
Marks m = new Marks();
Once we have created an instance for the class we can use this instance to call any method present inside the previous class. As we have declared a reference parameter in the method we will need to pass a reference variable. To do that, we use the declared object followed by a dot and then the name of the method that you want to access from the class instance.
The parameters are passed inside the bracket that is placed just after the method name. A semicolon is stationed at the end to denote the end of the line.
m.marksRecieved(ref english, ref maths);
Conclusion
Classes are the blueprint of a user-defined data type. It is used to group similar objects together. Objects are entities that define data and functionality. Objects are the runtime entity and are created to access all the members of the class.
The new class is declared by using the class declaration that starts with the keyword “class” followed by the name of the class. It can also specify modifiers or attributes of the class. All the members of the class are declared between two curly braces “{“ and “}”.
An instance of an object is created by using the “new” operator. It is denoted by using the class name for which the instance is being created followed by a variable to store the reference of the instance then a “=” equals to sign followed by the “new” keyword and then again the name of the class with both open and closed brackets “()”.
Sample Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication4 { class Marks { public void marksRecieved(ref int eng, ref int math) { //logical operations int total = eng + math; Console.WriteLine("Total mark is" + " " + total); Console.ReadLine(); } } class Student { public static void Main(string[] args) { /* local variable definition */ int english = 90; int maths = 85; //defining the object for the class marks Marks m = new Marks(); //calling marks total method for calculation m.marksRecieved(ref english, ref maths); } } }
=> Look For The Easy C# Training Guide Here