Introduction to Objects in C#
C# language is vastly used for windows-based games and desktop applications. Apart from that, it is also very popular, while building mobile applications and web applications. C# is the fully object-oriented language. With the constant update of frameworks of Microsoft’s .net core, it is always ready to support most of the required techniques to implement.
Table of contents
Basic Principles of Programming Language
- Before jumping to create a program, it is very crucial to understand the basic terminology and principles of a programming language. Before going into details for objects in C#, we need to know what the class is. The class determines what type of functionality their objects will have.
- It also allows creating a group for different types of variables. One must have a basic idea of inheritance, abstraction, polymorphism, and encapsulation; those are four building blocks of object-oriented programming.
- While creating an application, whenever we need to configure and allocate a block of memory, we need an object there. For example, in real life, a bike is an object, which has methods such as gear and acceleration; it has attributes such as color and mass volume, etc. The object is a structure which is having behavior and state.
How to Create Class & Object?
To create an object, we need to give a specific name for the class, which would be followed by the name of the object.
Let’s first create the Class called Bike.
1. Syntax to Create the Class
Class Bike
{
String Color = black
}
We have created the class Bike, so now let’s use this class to create objects.
2. Syntax to Create the Objects
class Bike
{
string color ="black";
static void Main(string[] args)
{
Bike myObj = new Bike();
Console.WriteLine(myObj.color);
}
}
To print the value of color, we have created an object called ColObj.
We can create multiple objects too.
3. Syntax to Create the Multiple Objects
class Bike
{
string color ="black";
static void Main(string[] args)
{
Bike colObj1 = new Bike();
Bike colObj2 = new Bike();
Bike colObj3 = new Bike();
Console.WriteLine(colObj1.color);
Console.WriteLine(colObj2.color);
Console.WriteLine(colObj3.color);
}
}
Example to Implement Objects in C#
Below is the example of Objects in C#:
Code:
using System.IO;
using System;
namespace Academy
{
class CBResearch
{
static void Main(string[] args)
{
Users user = new Users(26,"Reshav Agarwal");
user.GetUserDetails();
Console.WriteLine("Press A Key to Exit..");
Console.ReadLine();
}
}
public class Users
{
public int Age {get;set;}
public string Name {get;set;}
public Users(int age,string name)
{
Age = age;
Name = name;
}
public void GetUserDetails()
{
Console.WriteLine("Age:{0},Name:{1}", Age, Name);
}
}
}
We have included member functions and data members under the new class called ‘Users’. To populate methods and properties of the Users class, an object called ‘user’ has been created so the operation can be performed.
While executing the program the result would appear as below.
Output:
We will create classes and objects as per the criteria or based on our specific requirements.
Conclusion
Objects and Classes both are equally very much important while coding an application. There can be many numbers of objects in a class, and there is no need to share your objects with other classes. If another class is interested, then that will coordinate with its objects. Object-oriented programs are very easy and fast to execute. It also defines a clear structure for the program which further helps to modify, debugging and maintaining the codes, thus makes troubleshooting much easier. Like the use of inheritance, code can be reused, therefore reduce development time. Polymorphism gives major flexibility with writing codes. With the help of an abstraction mechanism, we can hide some data from exposure, which strengthens the security of that application. Encapsulation allows a developer to implement the required level of abstraction.
Recommended Articles
This is a guide to Objects in C#. Here we discuss the introduction to objects in C#, how to create class and object along with code implementation. You can also go through our other suggested articles to learn more –