Home  >  Article  >  Backend Development  >  Objects in C#

Objects in C#

王林
王林Original
2024-09-03 15:04:20400browse

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:

Objects in C#

  • 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:

Objects in C#

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:

Objects in C#

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:

Objects in C#

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.

The above is the detailed content of Objects in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Iterators in C#Next article:Iterators in C#