How to define a class in C#

oop

Hello friends!

Sorry, I haven’t written for a long time in my blog.

So… In the last post I wrote about extension methods and I mentioned the word class and may be some of you don’t know what the class in programming is, so I decided to tell you what the word class is and how to define class using C#.

What the class is?

The class in the Object-oriented-programming (OOP) is called a definition of given type of objects from the real-world. The class represents a pattern which describes different states and behaviours of the certain object. Each class has its own name and contains in its body the following elements: Fields, Constructors, Properties and Methods;

Fields – they are variables, declared inside the class. They keep data which shows the state of the object and they are needed to methods of the class. The fields show specific states of given object but in OOP exist fields called static fields which are shared among all the objects.

Constructor – this is something as method. We use the constrictor to create a new objects. The name of the constructor must be the same as the name of the class. And if the names of the constructor and the class are different you will receive error message before compilation.

Properties – they describe the characteristics of a given class. The value of the characteristics is kept in the fields of the object. Through the properties we can have access to the fields of the certain object

Methods – the methods are a code block which contain series of statements.  They define the behaviours of the instances of the class.

I hope you understand everything which is wrote above. But theory without examples is nothing. So it is time for example.
Now I am going to define class called Student which is description of the real object student.
In this class I have two fields, one constructor, two properties for every field and one method.

public class Student
{
//Fields
private string fullName;
private int age;


//Constructor
public Student(string fullName, int age)
{
this.fullName = fullName;
this.age = age;
}


//Properties
public string FullName
{
get { return this.fullName; }
set { this.fullName = value; }
}


public int Age
{
get { return this.age; }
set { this.age = value; }
}


//Method
public void IntroduceYourself()
{
Console.WriteLine("My name is {0} and I am {1}.", this.fullName, this.age);
}
}

There are some key words which I didn’t mention and these words are this, public and private.

The word this is keyword for language C#. In our class we use this word to make access to fields. You can make access to fields without to use this but it is good practice. The keywords public and private are an access modifier words. When you use public for the class, or the constructor, or the fields, etc. you say access is not restricted. It means everything which is public you can be access but when you use private the access is limited i.e. everything is accessible within certain class definition. It is good practice to use private for fields.

OK now I am going to show how my class Student worked.
In my Main() I make two students first and second and give them name and age. Then I called method IntroduceYourself() for each student.

static void Main()
{
Student firstStudent = new Student("John Roberts", 21);
Student secondStudent = new Student("Maria Adams", 19);


firstStudent.IntroduceYourself();
secondStudent.IntroduceYourself();
}

Output:
My name is John Roberts and I am 21.
My name is Maria Adams and I am 19.

I hope you understand everything. If you have some questions you know how to connect with me. BYE 🙂

3 thoughts on “How to define a class in C#

  1. can you explain how classes are used then they are in separate files, because as I understood it is better that each class has its own file. in example pascal has Uses file.pas; , c++ has include. so how does this work in c#?

    • Yes it is true that each class has to be defined in its own file. If you want to access your new class, it has to be defined with ‘public’ keyword at the begining (for example: public class Student { }). But if the class is defined in another folder in your project you have to add namespace of the folder where your class is defined. For more information look HERE

Leave a comment