Introduction
Class
Class can be defined as a collection of similar kind of objects.
Class can be defined by various ways:
- Conceptually
- Programmatically
- It is a classification.
- Customization - creates according to our need.
- Aggregation - for various object.
- Logical entity - because it doesn't takes any memory.
- Type for Object - We can make object of class types.
- Class is a keyword.
- Class consists - member function & member variable.
- Class keyword knows only two operators
--Assignment operator (=)
--Ampersand (&)
Syntax of Class
class
ClassName
{
// member variables ...
// member function ...
}
In C#, we have many types of
classes
Abstract class
Program of Abstract class
using
System;
namespace
abstractDeep
{
abstract class
Car
{
string regno;
int modelno;
public int
Model // This is property 1st
{
set { modelno =
value; }
get { return
modelno; }
}
public string
Resgistration // This is property 2nd
{
set { regno =
value; }
get { return
regno; }
}
public void
brakes() // function
{
Console.WriteLine("Brakes
are important to drive safely");
}
}
class Maruti:Car
{
public static
void Main()
{
Maruti mr =
new Maruti();
Console.WriteLine("
Maruti CAR DESCRIPTION");
Console.WriteLine("*******************************************");
mr.Model = 2005;
mr.Resgistration = "UP-14 Z 6224-DEEPAK";
Console.WriteLine("CarModel
: " + mr.Model + "");
Console.WriteLine("CarRegistration
: " + mr.Resgistration + "");
mr.brakes();
Console.WriteLine("*******************************************");
Console.Read();
}
}
}
Output

Scenario of Abstract class
If we have to make Cars with some same features then, we make the abstract
class then inherit this class with other class of car which can company name
(maruti,indica,Tata etc.).Here we can't make the object of Car class but we can
make object of maruti cars like Wagon, maruti 800.
Partial class
A class which is distributed over no. of files known as partial class. When
we want to create a single class distributed over a number of files then
we use the concept of partial class.
partial - keyword is used for making the class partial.
Syntax of Partial class
public
partial class
Student
{
public void
DoHomeWork()
{
}
}
public
partial class
Student
{
public void
GoToLunch()
{
}
}
Scenario of Partial class
It is helpful in case when we make a class with many function then no. of
person are assigned for that work and they make partial class with same name.
After compilation they can be combined into a single .dll (dynamic link
library).
Sealed class
Program of Sealed class
using
System;
namespace
Sealednmsp
{
class Car
{
string regno;
int modelno;
public int
Model // This is property 1st
{
set { modelno =
value; }
get { return
modelno; }
}
public string
Resgistration // This is property 2nd
{
set { regno =
value; }
get { return
regno; }
}
}
sealed class
WagonR:Car
// WagonR is sealed class so u cant inherit class
WagonR further
{
public static
void Main()
{
WagonR mr =
new WagonR();
Console.WriteLine("
WagonR CAR DESCRIPTION");
Console.WriteLine("****************************************");
mr.Model = 2005;
mr.Resgistration ="UP-14 Z
6224-GHAZIABAD";
Console.WriteLine("CarModel
: " + mr.Model + "");
Console.WriteLine("CarRegistration:
" + mr.Resgistration + "");
Console.WriteLine("****************************************");
Console.Read();
}
}
class
Final : WagonR
//Error 1 'Sealednmsp.Final': cannot derive from sealed type
'Sealednmsp.WagonR'
{
}
}
Scenario of Sealed class
If we have a car wagon R ,we must make it sealed so that no one can inherit
it to use according to his need. One can only have object of Wagon R cars.
Static class
Syntax of Static class
//
static class
static
class CollegeStu
{
public static
void DoSomething()
{
Console.WriteLine("Be
prepare for the exam");
}
public static
void DoSomethingExtra()
{
Console.WriteLine("Being
a topper of college need to do hard work with smartness");
}
}

Umang GoelPosted Oct 20, 2011, 5:44 AM
Hello deepak...its an awesome post...i have also tried some other books...but can't able to find the usual stuff in them............now its clears to me.......c-sharpCorner rocks!!!!!!!!!!!!
govind singhPosted Oct 20, 2011, 5:42 AM
It's great to read this topic of class . The way in which this topic define is better .I find more useful for me from this post. Thanks Deepak