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.
The above is the detailed content of Objects in C#. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

There are several ways to modify XML formats: manually editing with a text editor such as Notepad; automatically formatting with online or desktop XML formatting tools such as XMLbeautifier; define conversion rules using XML conversion tools such as XSLT; or parse and operate using programming languages such as Python. Be careful when modifying and back up the original files.
