Free Web Development Online Tutorials, Learn to Code
Class in C#, Create new object of C# Class, Add Property, Methods, C# a object type

C# class and object example

Object is real-time business entity, that can be anything like school, pencil, pen, paper etc. but in object oriented programming object is an instance of a class definition, Here you learn C# Class and Object concept with examples.

c# class and object, property, methods

To understand object, just think of any business entity and it’s state, behavior, identity, and how to deal with real time scenario.

In this Article we learn about Class, object, property, methods in C# .Net.
A class is a type of blueprint of any business object, that define a particular entity of any business with all attributes and functionalities.
Let's look at some Examples.

C# Class Example

Here we are working for a online training company called "WebTrainingRoom", we are into training business, here you can see i have created a class called Training, that contains all training related properties, methods etc.

namespace WebTrainingRoom
{
public class Training
{
public Training()
{
}
public string Subject { get; set; }
        public string Location { get; set; }
        public string Trainer { get; set; }
            
        public void Schedule()
        {
            
        }
        public void SendInvoice ()
        {
        }
    }
}

Please look at the picture below

c# class example

What is Object in C#?

Object is an instance of a class, let’s see an example.

Right now we have created Training class, now I as a student want to send a training request, in system we have another class called Student. So, from Student class we create a new instance of class Training, here is how..

public class Student
{
public void RequestTraining()
{
Training _training = new Training();         
    _training.Location = "Nariman Point, Mumbai";
    _training.Subject = "Asp.Net, C#, SQL";
            
    _training.Schedule();            
    _training = null;
    }
}

When a new object is instantiated with new keyword , it is allocated with a block of memory location on heap (because class is reference type). Object is also a keyword in C# (System.Object).

Here are few things to learn from c# object example:

  • c# object declaration
    Student s=new Student();
    
  • C# object type
    Student s=new Student();
    Type t= s.GetType();
    
  • c# object dispose
    Student s=new Student();
    s.Dispose();
    s =null;
    
    Learn how to implement IDisposable Interface

C# object Constructor Example

We can have multiple constructors in a class, also known as constructor overload

public Training()         
{
          
}
            
public Training(string TrainingName)
{
}

C# object Property Example

We can have any number of properties with any type of access modifier

public  class Student
{
    public string Subject { get; set; }             
    private string Location { get; set; }             
    public DateTime TrainingDate { get; set; }             
    protected string Trainer { get; set; }
}


// example how to access property
Student s=new Student();
s.Subject="C# Object Property";

C# object Methods Example

We can have any number of methods with any type of access modifier and return type

public class Training 
{
    public void Schedule()
    {   
        
    }
           
    protected void SendInvoice()
    {

    }
}

// example how to access method
Training tr=new Training()
tr.Schedule();

C# Namespace Example

Namespace is basically logical boundary name of any assembly, or scope of class, in .NET Framework namespaces are used for organizing many classes under it

namespace WebTrainingRoom             
{ 
    public  class Student
    {
    }
}

So when we use our Training class from another class, we have specify the namespace like using WebTrainingRoom;

Another example, suppose we want to use List class (ex. List<string> _Colors = new List<string>();), then we need to have reference of Generic namespace like using System.Collections.Generic; in our program

Comment
Name
Email
Website
Subscribe
 
free C# tutorial online


Learn Class, Object, Property, Methods in C#
Other Online Tutorial

C# .net Interview Questions Answers

Learn C# .Net Programming Online
.Net C# Examples | Join .Net C# Course