In this article we are going to discuss the object in C#. There are many blogs, articles are available on the internet regarding object but in this particular article I will try to explain to you with as much as simplest and realistic examples so you can get a clear idea of the object in C#.
Object Introduction
An object has an identity, state, and behavior as defined below with example.
Instantiation - The process of creating an object from a class is known as instantiation.
Example
A Person(Man) is a real-life Object, which has some characteristics like age, color, sleep, and eats, walk.
Creating Object using New Keyword
Let's see an example to create object using new keyword.
Person person1 = new Person();
In this example, Person is the type and person1 is the reference variable that refers to the instance of Person class. The new keyword allocates memory at runtime.
New Keyword in C#
In C#, the new keyword is used to create instances (objects) of classes. When you use new, you're essentially telling the compiler to allocate memory for a new instance of the class and then call its constructor to initialize that instance. Here's why the new keyword is necessary and what it does:
WithOut New Keyword in C#
The reference can be declared only with the class name and reference name. The reference cannot exist independently. It has to be assigned to an already existing object of the same class. Any changes made in the reference will be saved to the object it is referring to. It is kind of like an alias.
WithOut New Keyword Example
// C# Program to show the use
// of references
using System;
namespace Reference
{
class Triangle
{
public int side, altitude;
// Not defining a constructor
// Method to calculate area
// of the Triangle
public double Area()
{
return (double)0.5 * side * altitude;
}
}
// Driver Class
class Program
{
// Main Method
static void Main(string[] args)
{
// Creating an object using new
// calls the default constructor
Triangle tri1 = new Triangle();
// Only creates a reference of
// type Triangle
Triangle tri2;
// Displaying area of tri1
Console.WriteLine("Area of tri1 is "
+ tri1.Area());
// Assigns the reference to tri1
tri2 = tri1;
// Making changes in tri2
tri2.side = 8;
tri2.altitude = 9;
// Displaying area of tri1
// Changes made in the reference tri2
// are reflected in tri1 also
Console.WriteLine("Area of tri1 is "
+ tri1.Area());
Console.WriteLine("Area of tri2 is "
+ tri2.Area());
}
}
}
Output
Object Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Example
{
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
public void Introduce()
{
Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old.");
}
}
class Program
{
static void Main(string[] args)
{
Person person1 = new Person("Rohatash", 30); //Creating Object of Person Class
Person person2 = new Person("Mohit", 28); //Creating another Object of Person Class
Console.WriteLine("person Name = {0} Age = {1}", person1.Name, person1.Age);
Console.WriteLine("person Name = {0} Age = {1}", person2.Name, person2.Age);
}
}
}
In the example above, person1 and person2 are instances of the Person class.
Output
Prevent object creation of a class in C#
Object creation of a class can be prevented by:
Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Example
{
public abstract class WordDocument
{
}
public static class PDfDocument
{
}
private class ExcelDocument
{
}
public class Program
{
static void Main(string[] args)
{
WordDocument objWordDocument= new WordDocument();
PDfDocument objPDfDocument = new PDfDocument();
ExcelDocument objExcelDocument = new ExcelDocument();
}
}
}
Output
Now move mouse over the WordDocument.