C# program to show how we can pass object as an argument to a function

Category > CSHARP || Published on : Saturday, November 7, 2020 || Views: 27 || pass object as an argument to a function


Here Pawan Kumar will explain how to show how we can pass object as an argument to a function

C# program to show how we can pass object as an argument to a function

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication13
{
    class Student
    {
        int rollno;
        string name;
        public void SetData(int r, string n)
        {
            rollno = r;
            name = n;
        }
        public void ShowData()
        {
            Console.WriteLine(rollno + " " + name);
        }
        public void SetData(Student a)
        {
            rollno = a.rollno;
            name = a.name;
        }
    }
    class Demo
    {
        static void Main(string[] args)
        {
            Student s = new Student();
            s.SetData(1, "Shirlee");
            s.ShowData();
            Student t = new Student();
            t.SetData(s);
            t.ShowData();
            t.SetData(2, "Robin"); // s will not change
            t.ShowData();
            s.ShowData();
            Console.ReadLine();
        }
    }
}