• Startseite
  • Tutorials
  • Kontakt
  • Mein Account
Panjutorials
  • Startseite
  • Tutorials
  • Kontakt
  • Mein Account

Our first own class in CSharp

In this article, we create our first own class in CSharp. In the last lecture, we have already dealt with what classes are about. Now we create our own class and look at how attributes and actions work.

Example for classes in CSharp

In order to create a new class within the Solution Explorer right click on your already existing class by right-clicking, then add-> new Item -> Class. Name the class Person.cs

Here you can see the class with the comments explaining what the code does.

 

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

namespace Panjutorials
{
    class Person
    {
        // attributes of the class Person
        int age = 28;
        string firstname = "Denis";
        string lastname = "Panjuta";
        string eyeColor = "Grün";

        // This is an example of an introduction
        public void introduceOneSelf()
        {
            Console.WriteLine("My name is " + firstname + " " + lastname + " and I am " + age + " years old " +
                " and my eye color is " + eyeColor + ".");
            Console.Read();

        }

    }
}

 

So you see here that we have a class, starting with the term class, which we called Person. Then, at the very top, we give the attributes of such aPerson class, and here we have only one example of an action, with which the person presents himself with his data when we call the function on the object of that class. We will discuss how to do this in the next article.

If we want to create an object from this class, we need a constructor. How exactly a constructor works and how to use it, will be covered in the next article.