Press "Enter" to skip to content

C# – Class

In C# , We can define the class and thanks to this class we can define the objects. In defined class, we can fill the objects later. In this essay, I will talk about the using the class. You can also understand from the code sequence. When we define the class, we should determine the class name because we call this class with the class name. If you make a class about the school, you can write class school. After that you can fill the inside of class. For example, for the get student name you should define in the class. public string student with this term you can define. In the class, you can also define the objects private but for the get private terms you should use get and set properties. We use ‘get‘ for the turn value and We use ‘set‘ for the arrange value.

In the program, I will define a class and the class name is school. In this school class, I will define the name, surname and school number. Code Sequence;

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

namespace ConsoleApp1
{
    class Program
    {
        class school
        {
            public string name;
            public string surname;
            public int schoolnumber;

        }
        static void Main(string[] args)
        {
            school student = new school();
            student.name = "Ebubekir";
            student.surname = "Sezer";
            student.schoolnumber = 3030;
            Console.WriteLine("Name: " + student.name);
            Console.WriteLine("Surname: " + student.surname);
            Console.WriteLine("Number:" + student.schoolnumber);
            Console.ReadKey();
        }
    }
}

Screen Output;

Name: Ebubekir
Surname: Sezer
Number:3030

Now, I will make the same thing like the above but this time I will define the school number private and I will use get and set properties. Code Sequence;

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

namespace ConsoleApp1
{
    class Program
    {
        class school{
            public string name;
            public string surname;
            private int schoolnumber;
           public void setSchoolnumber(int schoolnumber)
            {
                if (schoolnumber < 0)
                {
                    this.schoolnumber = 0;
                }
                else
                    this.schoolnumber = schoolnumber;

            }
            public int getSchoolnumber()
            {
                return schoolnumber;   
            }

        }
        static void Main(string[] args)
        {
            school student = new school();
            student.name = "Ebubekir";
            student.surname = "Sezer";
            student.setSchoolnumber(3030);
            Console.WriteLine("Name: "+ student.name);
            Console.WriteLine("Surname: "+ student.surname);
            Console.WriteLine("Number:"+ student.getSchoolnumber());
            Console.ReadKey();
        }
    }
}

Screen Output;

Name: Ebubekir
Surname: Sezer
Number:3030

As you see we can make the program usable with the get and set properties.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *