C# program that will work like copy constructor of c++

Category > CSHARP || Published on : Sunday, November 8, 2020 || Views: 789 || copy constructor of c++ in C#


Here Pawan Kumar will explain C# program that will work like copy constructor of c++

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
36
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication14
{
    class Student
    {
        int rollno;
        string name;
 
        public void SetData(int r, string n)
        {
            rollno = r;
            name = n;
        }
        public void ShowData()
        {
            Console.WriteLine(rollno + " " + name);
        }
    }
   class Demo
    {
        static void Main(string[] args)
        {
            Student s = new Student();
            s.SetData(1, "Rocky");
            s.ShowData();
            Student t = s;// t will point to s
    t.ShowData();
            t.SetData(2, "Lincon");// s will also be changed                            
t.ShowData();
s.ShowData();
            Console.ReadLine();
        }
    }
}