Online Training for Asp.net MVC Web Designing Development, MS SQL, Digital Marketing

Class, object, property, methods in C# .Net

Learn Class, object, property, methods in C# Programming

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).

C# Constructor Example

We can have multiple constructors in a class, also known as constructor overload
public Training()
{
}

public Training(string TrainingName)
{
}

C# Property Example
We can have any number of properties with any type of access modifier
public string Subject { get; set; }
private string Location { get; set; }
public DateTime TrainingDate { get; set; }
protected string Trainer { get; set; }

C# Methods Example
We can have any number of methods with any type of access modifier and return type
public void Schedule()
{
}

protected void SendInvoice ()
{
}

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
{
}
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

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



Learn C# .Net Programming Online
Group Training