Creating your first class using C#

Let’s increase some speed of our C# learning journey and learn about a very important concept in C# - Classes. C# is a purely object oriented language. This means that in C# nothing can exist outside a class. Each and everything has to be inside a class. Somebody can also argue that you can create a structure instead of creating a class in C# and this argument is not wrong, but at the same time it is not 100% correct. Object oriented programming revolves around classes and not structures. Although majority of the concepts you will learn about classes in C# will apply as it is to structures also, still we learn and use classes.

How do we define a class?

A class is defined as a blueprint or a template of the object you want to create. Suppose you want to create an object of the type Student using C#. Do you think C# knows what a Student is? No. You will first have to create a blue print of the Student object and tell C# what data and functions you want in this object. Once you create your blueprint C# will know what you mean by a Student and it will be able to create the object of the type Student when you ask it to.

Syntax to create a class using C#

The syntax to create a class using C# is very simple. Also the good news is that the same syntax is used in Java to create a class. Look at the code below

class Test  {    }

Above we have created a class called Test. You can name your class whatever you want. Say you want to create a class called Football, the syntax for it will be

class Football  {    }

We have kept both these classes empty for now. What you should learn at this point is the syntax to create a class. In the coming tutorials we will teach you how to add data and functions to your class.

User defined data types

C# comes with a large number of pre defined data types like int, char, float, double, string etc. C# also gives you the power to create your own data types using classes and structures. So when you create a class you are actually creating a new data type. You can then use this data type to create variable in just the same way you use pre defined data types to create variables.

Opening and closing brackets

You might have noticed that we are using opening and closing curly brackets when writing the class. We use these brackets to define the boundary of our class. Any data or functions we write inside these brackets will be considered a part of the class. These brackets act as indicators for the compiler to know the starting and ending of your class.

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp