.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Post New Resource Bookmark and Share   

Exploring Class in C#

Posted By:Ravi Ranjan Kumar       Posted Date: January 29, 2012    Points: 25    Category:    URL: http://www.dotnetspark.com  

This article enable you to learn the basics concept of Class.


As we know C# is a true object-oriented language, therefore everything must be placed inside a class. Basically a class is a user defined data type with a template that serves to define its properties. In short class is nothing it's just a kind of template for object (as what an object look like and how it behaves) which we create in program, a kind of specification or blue print which must be followed by object created for correspondence class.

How to create class

Class classname


//here define member of class

[variable declaration;]

[methods declarations;]



While creating class the first step is came which is class keyword and then name of class or identifier.  After declaring class name the next step is declaring body of class which will start with opening curly braces { and also end with closing curly braces }. We need to declare body inside opening and closing curly braces.

 Class program{ } 

 Here class is a keyword ad program is valid C# identifier.

Class and structs are very similar but class is a reference type. Also classes include more features compared to structs. 

Have a look at some categories of class members

Note :-  C# classes are reference types, that's why the allocation of class doesn't exist in Stack class are always allocated on the Managed Heap. The Managed Heap is an area of memory that is managed by the CLR( Common Language Runtime), which has the ability to free unused memory blocks (objects) in a process known as Garbage Collection.

The Class Name :- 

The class keyword is followed by the name of the class keyword. in the given code Calculator is the class.  While creating class we must consider naming convention and rules

Class Naming Conventions in C#

Class names should follow certain  naming conventions or guidelines. A class name :

·         a) Should be meaningful(strongly recommended)

·         b) Should ideally be a noun.

·         c) Can use either the Pascal case or Camel Case.

mhave a look there-[http://msdn.microsoft.com/en-us/library/x2dbyw72%28v=vs.71%29.aspx]  see for more information

Rules for Naming classes in C#

Name of Classes

a·     a)  Must begin with a letter. This letter may be followed by a sequence of letters, digits (0-9) or "-". The first character in a class name cannot be a digit.

·        b)  Must not contain any embedded space or symbol like  ?-+!@$%^&*(){}[],;"'/ and \ however an underscore ('_') can be used wherever a space is required.

·        c)  Must not use a keyword for a class name. For example you can't declare a class called public.


Have a look at program