Introduction to classes in C#

By Aug 17, 2017

Description:

We here explain the concept of class in C# and give some examples. The C# language is object-oriented and the programming is based on classes.

Preferencesoft

In this article, we introduce the notion of class in C# and we explain how to program with classes in C#.

C# classes

The C# language is an object-oriented language and it is almost impossible to program without noticing the presence of the class keyword in the minimal code of a console program.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.ReadLine();
        }
    }
}

This word class marks the beginning of a class declaration and is of great importance in C# because all code in a program is distributed in class member functions.

Those who start in programming can start by learning basic instructions by working only in the program class. They will be able to try small simple programs.

But you must be interested in the concept of class sooner or later to better control programming in C#.

Defining a Class

A class is a data type that can contain declarations of variables, functions, properties, new classes, ...

Eventually, a class contains member functions with the same name as the class, called constructor, and whose role is to automatically initialize certain variables.

In C#, classes are paramount since any program consists of class declarations referencing others.

Different types of classes are distinguished: non-static classes and static classes.

Classes (non-static) are used by creating class instances called objects, that is, by creating a memory block of type the considered class. An object is created with the reserved word new, followed by a constructor of the class and parentheses that may contain parameters. The object can then be referenced.

Other static classes cannot be instantiated, and to call a member function of such a class, the name of the class must be followed by the name of the function and separated by a dot.

Objects and classes

We will consider in this part only non-static classes.

First, it is important not to confuse a class with an object. This would mean confusing the int type for example with any integer value.

If we have defined a class C1 for example, and the remainder of the program does not reference it then when the program is run, class C1 or any of its member functions will never be "executed".

To be able to use C1 in the main function for example, an object of class C1 must be created. This has the effect, at the time of execution, to create a data block in memory, modelled on the class model (with placeholders, references to functions, and so on) and perform some initialization by calling the default constructor or the one that was called during instantiation.

Programming purpose

This hyper-structuring of a program can destabilize programmers who practice procedural programming with languages such as C, Pascal, Basic, ... In addition to having to think about the organization of the data, we also must think about the code. But this object-oriented programming is very good for those who want to program in a modular way.

The need for object-oriented programming is felt when you need to write programs that have a graphical user interface and you need to manage objects such as menus, dialog boxes, and so on. It is indeed easier in this type of programming, to create objects, to modify certain properties of objects or their behavior etc.

The thread of a program

To be able to understand a C# program in console mode, you must locate the main function of the program class.

When a console program starts, an object of the program class is created and the main member function is called.

Sample program

When you create a program in console mode with Visual Studio, a default generated Program.cs file contains the code above. Let's add some instructions in  program to create the book class, two books and display their title and author name:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        class Book
        {
            string _author;
            string _title;
            string _publisher;
            int _pageNumber;
            public Book(string title, string author, string publisher, int pageNumber)
            {
                _title = title;
                _author = author;
                _publisher = publisher;
                _pageNumber = pageNumber;
            }

            public void displayBook()
            {
                Console.WriteLine("Title: " + _title);
                Console.WriteLine("Author: " + _author);
            }
        }
        static void Main(string[] args)
        {
            Book b1 = new Book("b1","A1","",10);
            Book b2 = new Book("b2", "A2", "", 10);
            b1.displayBook();
            b2.displayBook();
            Console.ReadLine();
        }
    }
}

This code corresponds to the program class containing the main function executed first (internally). Our class is created inside program class.


CSharp

Categories

Share

Follow


KodFor Privacy Policy