Classes in C#

Posted by in C# category on for Beginner level | Views : 16074 red flag

Classes are the irreplaceable ingredient of any .NET assembly. A Class is the generic definition of what an object is a template. The keyword class in C# indicates that we are going to define a new class (type of object)

A class declaration consists of class declaration and class body. Class header includes attributes, modifiers, "class" keyword, the class name and the base class list.

attribute accessibility modifiers class classname: baselist

// Class Body


Lets see what each of these header means.

attribute describes the class. eg. Serializable attribute identifies a class that can be serialized to store physically on the disk. We have freedom to define our own custom attributes.

accessibility describes the visibility of the class. eg. Public, Internal etc. Public classes are visible in the current assembly as well as the assembly referencing it. By default if no accessibility is defined, Internal is the default class accessiblity that means this class will only be visible in the current assembly.

modifiers filters the declaration of the class. eg. Sealed keyword prevent the class to be inherited.

Modifiers Descriptions
Sealed A class can't be inherited
Abstract Instance of the class can't be created
static Class can only contain static members (methods, variable etc.)
Unsafe The unsafe keyword denotes an unsafe context, which is required for any operation involving pointers.

class is the keyword that is used to define a class.

classname is the string that is used to name a class we are going to define.

baselist is the list of class or interface that we are going to inherit in this class. C# doesn't support multiple inheritence so we can only inherit one class; however, we can inherit multiple interface. In case of inherting multiple inheritence we can write them as comma separated. If no class is inhertited, by default a class inherit System.Object.

Class Member

A class can contain members in the form of member function or data member. Members can be of several types eg. Methods, Properties, Fields, Constants, Constructors etc.

public class myClass


int i = 0;

protected string GetMyName()


return "MyName";



We can define the visibility of the member using accessibility keywords. Private is the default accessibility for the class member.

Accessibility Descriptions
internal Member is visible in the containing assembly only
internal protected Member is visible in the containing assembly or visible inside current class and any descendants
private Member visible inside the current class
protected Member visible inside current class and any descendants
public Member visible in current assembly as well as other assembly referencing this assembly

Members can contain the modifiers as well that refine the definition of the member. These modifiers are optional and there are no default modifiers. Following is the list of modifiers that are frequently used.

Modifiers Descriptions
abstract Member function has no implementation and is implemented through inheritence
new Hides the similar member or member in the base class
override Used to override a virtual member in the base class
readonly Can't be set its value except at the time of declaration or in the constructors
sealed This member can't be inherited
static This member belongs to the class not to the instance of the class
virtual This member is overridable in the derived class.

Hope this will be helpful in understanting about a class. Thanks for reading.

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan

Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on | |

Login to vote for this post.

Comments or Responses

Posted by: Poster on: 12/18/2008
Thanks for the well explained article on Class, this solved many of the doubts related with modifiers and accessibility related issues.

Thanks and keep it up!!!

Login to post response

Comment using Facebook(Author doesn't get notification)