This Angular tutorial helps you get started with Angular quickly and effectively through many practical examples.

Object


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

  1. An object is an instance of a class. All the members of the class can be accessed through object.
  2. Object is a runtime entity, it is created at runtime.
  3. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated.
  4. It contains properties (attributes) to hold data and methods (functions) to perform operations on that data.
  5. Every object in C# is created from a class template that defines its structure and behaviors.
  6. In C#, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc.

An object has an identity, state, and behavior as defined below with example.

  1. State - Defined by the object's properties/fields. It represents the data held by the object.
  2. Behavior - Defined by the methods within the class. These are actions that the object can perform, often manipulating its own properties or interacting with other objects.
  3. Identity - Every object has a unique identity, which allows it to be distinguished from other objects, even if they share the same properties.

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.

Object

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:

  1. Memory Allocation - When you create an object, you need to allocate memory to hold its data and methods. The new keyword triggers this memory allocation process. Without new, the object would not have memory space to exist.
  2. Constructor Invocation - After memory allocation, the constructor of the class is called to initialize the object. Constructors are special methods that have the same name as the class and are used to initialize the object's state. The new keyword ensures that the constructor is invoked during object creation.
  3. Returning a Reference - The new keyword returns a reference (or pointer) to the newly created object. This reference allows you to access and manipulate the object's properties and methods throughout your program.

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

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

Object

Prevent object creation of a class in C#

Object creation of a class can be prevented by:

  1. Abstract Class
  2. Private Class
  3. Static Class

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.

Object
Next