内部静态类C#没有静态嵌套类 [英] none static nested class inside static class c#
问题描述
考虑此方案
Public static Class GlobalParam
{
//static classes strings int and more...
class NoneStaticClass
{
//some noneStatic params
}
}
在另一个类
(无静态)我呼吁的一个实例 NoneStaticClass
以这种方式
in another class
( none static ) I call for an instance of NoneStaticClass
in this fashion
GlobalParam.noneStaticClass NSC = new GlobalParam.noneStaticClass();
//some manipulation on NSC params
后来我用的方法是一样
Later I use Method Like that
void DoSomething(GlobalParam.noneStaticClass nsc)
{
GlobalParam.noneStaticClass NewNSC = nsc
//Some manipulation in NewNSC
}
现在,当我检查存储在NSC数据我可以明确地,它已经改变了,这是为什么?
并没有把静态类的静态里面是不是以某种方式正确吗?
now when I check the data stored in NSC I can clearly that it has been changed , why is that ? does putting none static class inside a static is not correct in some way ?
推荐答案
无论你的类嵌套或正常类,只要你做的像一个assignement GlobalParam.noneStaticClass NewNSC = NSC
, NewNSC
和 NSC
将始终引用同一个对象。
Whatever your class is nested or normal class, whenever you do an assignement like GlobalParam.noneStaticClass NewNSC = nsc
, NewNSC
and nsc
will always reference the same object.
如果您希望您的NSC对象的副本,这里有一些方法来做到这一点:
If you want a copy of your nsc object, here are some ways to do it :
- 如果您的类与值类型的所有成员,可以使用结构
,而不是类。它会在您每次进行分配
随时间复制。 - 如果没有,你应该引用者为对象克隆
- If your class has all members with value type, you can use a struct instead of class. It will be copied each time you make an assignment with.
- If not, you should referer to object cloning
这篇关于内部静态类C#没有静态嵌套类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!