C# to show how structure variable works differently than class variable (object)

Category > CSHARP || Published on : Thursday, November 5, 2020 || Views: 769 || structure variable works differently than class variable (object)


Here Pawan Kumar will explain how to show how structure variable works differently than class variable (object)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication12
{
    struct Student
    {
        int rollno;
        string name;
        public void SetData(int r, string n)
        {
            rollno = r;
            name = n;
        }
        public void ShowData()
        {
            Console.WriteLine(rollno + " " + name);
        }
    }
    class strt
    {
        static void Main(string[] args)
        {
            Student s = new Student();
            s.SetData(1, "Enrique");
            s.ShowData();
            Student t = s; // values of s will be copied into t
            t.ShowData();
            t.SetData(2, "Atif"); // s will not change
            t.ShowData();
            s.ShowData();
            Console.ReadLine();
       }
    }
}