Get the instance name of the object no the object type name in C# 4.0

后端 未结 3 1743
自闭症患者
自闭症患者 2021-01-13 02:49

Assume this type:

public class Car { } 

And I create an instance of that:

Car myCar = new Car();

Target target = new Targe         


        
3条回答
  •  无人共我
    2021-01-13 03:20

    No, this is not possible. Why? Because myCar is only a name for your Car instance inside the scope which you set up the variable for. What gets saved to target.Model is a reference to the Car instance. So, after your sample code, both myCar and target.Model reference the same instance of Car, just with different names. The names themselves are rather unimportant to the execution of the program, it's just a trick we use to make talking about instances easier.

提交回复
热议问题