2016-09-02 4 views
0

동일한 클래스의 인스턴스를 비교하십시오. 신구가 동일한 경우 동일한 클래스의 인스턴스를 비교하고 이전 값을 새 값으로 업데이트하십시오.

public class Student() 
{ 
    public string Name {get; set;} 
    public string Hobby {get; set;} 
    public string Country {get; set;} 
} 

Student old = new Student { Name = "A", Hobby = "Swim", Country = "HK" }; 
Student new = new Student { Name = "A", Hobby = "Jog", Country = "US" }; 

다른, 다른 클래스의 필드의 각 교체, 비교. 이전 값을 새 값으로 업데이트하는 것과 같습니다.

If(old != new) 
{ 
    UpdateOldWithNew(old, new); 
} 
+0

은 아마 당신은 함수 ....이 작업을 수행 만들어야합니다? 각 부분을 비교하면됩니다. –

+0

https://msdn.microsoft.com/en-ca/library/ms173147(v=vs.90).aspx – Derek

+0

클래스의 수정 된 값만 업데이트하면 제네릭 함수가 업데이트됩니까? – superhuman1314

답변

0
Student student1 = new Student { Name = "A", Hobby = "Swim", Country = "HK" }; 
    Student student2 = new Student { Name = "A", Hobby = "Jog", Country = "US" }; 
    Student student3 = new Student { Name = "A", Hobby = "Swim", Country = "HK" }; 

    //reference equal 
    System.Debug.WriteLine(student1 == student2); //false 
    System.Debug.WriteLine(student1 == student3); //false 
    System.Debug.WriteLine(student1 == student1); //true 


    if(student1 != student2) 
    { 
     student1 = student2; 
    }