class Program
{
delegate void MyDelegate();
static void Main(string[] args)
{
//1. string
string str1 = "Hello";
string str2 = str1;
str1 = "Shoot";
Console.WriteLine($"str1:{str1} str2:{str2}"); //str1:Shoot str2:Hello 참조타입이지만 값이 변하지않음
//2. delegate
MyDelegate myDelegate1 = () => { Console.WriteLine("myDel_First"); };
MyDelegate myDelegate2 = myDelegate1;
myDelegate1 = () => { Console.WriteLine("myDel_Edit"); };
myDelegate1?.Invoke(); //myDel_Edit
myDelegate2?.Invoke(); //myDel_First
}
}
값이 변하지 않는 것을 확인 할 수 있다.
class Animal
{
int age;
public void SetAge(int input)
{
age = input;
}
public void PrintAge()
{
Console.WriteLine($"동물나이: {age}");
}
}
class Program
{
static void Main(string[] args)
{
Animal animal1 = new Animal();
Animal animal2 = animal1;
animal1.SetAge(10);
animal2.SetAge(20);
animal1.PrintAge(); //동물나이: 20
animal2.PrintAge(); //동물나이: 20
}
}
animal1을 변경하면 animal2가 변경되는 것을 확인 가능