Properties (get and set) in structure in C#

In this example, we will figure out how to characterize set and get properties inside a C# structure? Here, we have an example that has set and get properties inside the structure.

In C#, we can characterize set and get properties in any structure and they can be gotten to through object of that structure.

For the most part, to set a value to private information individual from a structure, we utilize an open technique with the contentions, however utilizing set property we can legitimately allot value to that information part.

For example, there is an information part named roll_number, for this we make a set property named ‘Move’, at that point by utilizing object name we can legitimately dole out value to the roll_number like S1. Roll = 101; Where S1 is the object of structure.

Same usefulness with the get property, to characterize it we give a name of the appropriately and restores the value of private information part. Think about the given example.

Example:

using System;
using System.Collections;

namespace ConsoleApplication1
{
    struct Student
    {
        private int roll_number;
        private string name;

        public int Roll
        {
            get
            {
                return roll_number;
            }
            set
            {
                roll_number = value;
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
    }
    class Program
    {
        static void Main()
        {
            Student S1 = new Student();

            S1.Roll = 121;
            S1.Name = "Kishan Kumar Kaushik";

            Console.WriteLine("Roll NO: " + S1.Roll + "\nName: " + S1.Name);
           
        }
    }
}

Output

Roll NO: 101
Name: Kishan Kumar Kaushik

Leave a Comment

error: Alert: Content is protected!!