Introduction to Objects in C#
The following article provides an outline for Objects in C#. As already known, C# is an object-oriented programming language and is pronounced as C Sharp. Though C# has been evolved from C++, both differentiate from each other. The basic differences can be understood through C++ vs C#.
The object is an instance of a class. Here memory is allocated dynamically for providing the output of a given program. So, how can we explain this dynamic allocation? Objects are created to access different functions or variables that are defined under the class. So, an object does not know which data type it is actually going to access. So after getting the value from the accessed elements, it would arrange the memory dynamically.
Creating an Object
In general, an object can be created in 2 ways. One of them is by using the “new” command.
The general syntax for the object is below:
Class-name object-name = new Class-name();
And then, by using the object-name, we can access respective methods and variables that are defined inside the class.
Another way of defining an object is by reference to another object. Something like assigning the value.
Code:
Class-name object-name1 = new Class-name();
Class-name object-name2;
Object-name2=object-name1;
And we can access the variable and methods in the class using the objects object-name1 and object-name2.
Examples of Objects in C#
Here we are going to have one example for each way of creating an object in C#.
Below is a program for finding the square of a number.
Code:
using System;
class Square
{
public int side;
public Square(int a)
{
side=a;
}
public int Sq()
{
return side*side;
}
}
class First
{
static void Main(String [] args)
{
int result;
Square s= new Square(4);
result=s.Sq();
Console.WriteLine("Square of the given number is " + result);
}
}
Output:
- We have created a class Square and wrote two functions inside the class. One function, which is also a constructor (Function name same as for Class name), is for inputting in the value of a number and the other for doing the actual operation.
- In our class First which has the main function inside it, we have initialized our object ‘s’ and passed in the parameter, for which number we actually want to perform the square operation.
- And we declared a variable result; we are passing the output of the object accessed method ‘Sq’, which does the actual operation.
- Finally, outputting the square result in our console.
For our next way of creating an object, an example is as below:
Code:
using System;
class Square
{
public int side;
public Square(int a)
{
side=a;
}
public int Sq()
{
return side*side;
}
}
class First
{
static void Main(String [] args)
{
int result1,result2;
Square s1= new Square(4);
Square s2;
s2=s1;
result1=s1.Sq();
result2=s2.Sq();
Console.WriteLine("Square of the given number is " + result1);
Console.WriteLine("Square of the given number is " + result2);
}
}
Output:
And to an extension to this, we can even assign value to our variable using an object. Let’s see how we can do that.
Code:
using System;
class Square
{
public int Side;
public Square(int side)
{
Side=side;
}
public int Sq()
{
return Side*Side;
}
}
class First
{
static void Main(String [] args)
{
int result1,result2,result3;
Square s1= new Square(4);
Square s2= new Square(6);
result1=s1.Sq();
result2=s2.Sq();
s2.Side=7;
result3=s2.Sq();
Console.WriteLine("Square of the given number is " + result1);
Console.WriteLine("Square of the given number is " + result2);
Console.WriteLine("Square of the given number is " + result3);
}
}
Here, we have accessed the variable and changed its value from 6 to 7. Then the output is printed after initializing the value to the new variable result 3.
Output:
Till here, we have created an object and referenced it through a single text format. Now let us see what if we require an array of objects to store and manipulate our data.
Code:
using System;
class Square
{
public int Side;
public void Sqr(int side)
{
Side=side;
}
public int Sq()
{
return Side*Side;
}
}
class First
{
static void Main(String [] args)
{
int result1,result2,result3;
Square[] sq = new Square[3];
sq[0]= new Square();
sq[1]= new Square();
sq[2]= new Square();
sq[0].Side=13;
sq[1].Side=85;
sq[2].Side=25;
result1=sq[0].Sq();
result2=sq[1].Sq();
result3=sq[2].Sq();
Console.WriteLine("Square of the given number is " + result1);
Console.WriteLine("Square of the given number is " + result2);
Console.WriteLine("Square of the given number is " + result3);
}
}
In the above program, the same as before, we have created an array of objects and assigned a value to each object. We then executed our second function to generate the square of two numbers.
Output:
As an exercise, can you try loading in marks of 5 students in 3 subjects using an array of the object?
Conclusion
As seen above, we have successfully created an object in different ways and used it to assign values to variables and call the functions present inside the class. But here, we need to understand and follow a few rules based on the access modifiers. An object cannot access variables/functions with a “private” access modifier that belongs to another class. But can access the same class variables or functions though declared with a private modifier. So, this way, there are a set of rules that are defined with respect to classes, variables, functions, and objects. Try playing around in creating objects in different ways with different access modifiers and check out the outputs for getting to know the scope of objects and keep learning.
Recommended Articles
This is a guide to Objects in C#. Here we discuss the different ways of creating objects in C# along with examples and its code implementation. You may also look at the following articles to learn more –