C# 通过引用使用对象的方法
我正在尝试找出这段 ASP.NET C# 代码。有人可以建议我在这里不做什么吗?
当我运行下面的代码时,我期望 strDest 为“新值”并且 nDest 等于 2,但它们保持不变...
//These are two variables to fill
string strDest = "";
int nDest = 0;
public enum MyDataType
{
MyTypeString,
MyTypeInt
}
public struct MyStruct
{
public MyDataType type;
public Object objDest;
public MyStruct(MyDataType tType, Object oDest)
{
type = tType;
objDest = oDest;
}
}
//Pass variables by reference
MyStruct[] data2Check = {
new MyStruct(MyTypeString, strDest),
new MyStruct(MyTypeInt, nDest),
};
//And set them
for(int i = 0; i < data2Check.Length; i++)
{
if(data2Check[i].type == MyTypeString)
{
(string)data2Check[i].objDest = "New value";
}
else if(data2Check[i].type == MyTypeInt)
{
(int)data2Check[i].objDest = 2;
}
}
编辑: 好吧,让我重新表述一下。假设我有一组变量或一个类的成员,比如说 50 个。它们都是不同类型的。 (在此示例中,它们由字符串 strDest 和 int nDest 表示。)好的。现在我还有一个名称=值对的二维数组,因此每个对都应根据“名称”分配给我的类成员。我执行确定哪个“名称”属于类的哪个成员的过程,下面的 for() 循环是我对分配过程的抽象。似乎缺少的是通过引用将我的类成员传递到此 for() 循环中的方法,以便我稍后可以为它们分配值。
附言。到目前为止,下面的例子都没有完成这项工作......
I'm trying to figure out this piece of ASP.NET C# code. Can someone suggest what am I not doing right here?
When I run the code below I'm expecting strDest to be "New value" and nDest equal to 2, but they remain unchanged...
//These are two variables to fill
string strDest = "";
int nDest = 0;
public enum MyDataType
{
MyTypeString,
MyTypeInt
}
public struct MyStruct
{
public MyDataType type;
public Object objDest;
public MyStruct(MyDataType tType, Object oDest)
{
type = tType;
objDest = oDest;
}
}
//Pass variables by reference
MyStruct[] data2Check = {
new MyStruct(MyTypeString, strDest),
new MyStruct(MyTypeInt, nDest),
};
//And set them
for(int i = 0; i < data2Check.Length; i++)
{
if(data2Check[i].type == MyTypeString)
{
(string)data2Check[i].objDest = "New value";
}
else if(data2Check[i].type == MyTypeInt)
{
(int)data2Check[i].objDest = 2;
}
}
EDIT:
OK, Let me re-phrase it. Say, I have a set of variables or members of a class, say 50 of them. They are all of different types. (In this example they are represented by string strDest and int nDest.) OK. Now I also have a two dimensional array of name=value pairs, so each of those pairs should be assigned to my class members depending on the "name". I do the process of determining which "name" goes to what member of the class, and the for() loop below is my abstraction of the assignment process. What seems to be missing is the way to pass my class members by reference into this for() loop so that I can later assign values to them.
PS. So far, none of the examples below do the job...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
C# 处理引用与值的方式与 C++ 不同。在 C# 中,默认情况下,所有内容(甚至引用)都按值传递(另外:一些著名的 C# 开发人员不赞成可变结构,包括堆栈溢出代表领导者 Jon Skeet 和 C# 语言团队领导 Eric Lippert)。
要获得满足您需要的代码,您可以这样做:
但实际上,您从 C++ 世界中引入了一些对于 C# 毫无意义的习惯用法。坚持使用引用类型,并在类型可能发生变化时使用泛型。我不确定您要解决的具体问题是什么,或者我可以为您提供一些关于如何用惯用的 c# 表达解决方案的更具体的指导
C# doesn't handle reference vs value quite the same as C++. In C#, everything, even references, are passed by value by default (also: mutable structs are frowned on by several notable c# developers, including stack overflow rep leader Jon Skeet and C# language team lead Eric Lippert).
To get code that does what you're looking for, you do this:
But really, you're pulling in some idioms from the C++ world that make no sense at all for C#. Stick with reference types, and go with a generic when types might vary. I'm not sure what exact problem you're trying to solve, or I could give you some more specific guidance on how to express a solution in idiomatic c#
当您将
strDest
和nDest
传递给MyStruct
构造函数时,它们是按值传递的。对于值类型int
和引用类型string
,编译器生成的代码是不同的。对nDest
的值进行装箱(不会影响nDest
变量本身)。对于string
来说,类型不变性(或“写入时复制”行为)发挥了作用。When you pass
strDest
andnDest
toMyStruct
constructors, they are passed by value. The compiler generated code is different for a value typeint
and reference typestring
. There is boxing done on the value ofnDest
(that does not affect thenDest
variable itself). Withstring
it is the type immutability (or "copy on write" behaviour) that plays the part.我仍然没有遵循你想要做的事情......并且正如@JoelCoehoorn(和许多其他人)所说,你应该尝试使你的值类型(结构)不可变。这有点丑陋,但这里有一些有用的东西。如果我更好地理解你想要做什么,这可能会更清晰 - 我不提倡在生产中使用此示例:
输出:
I'm still not following what you are trying to do ...and as @JoelCoehoorn (and many others) states, you should try to make your value type (struct) immutable. This is kind of ugly, but here's something that works. If I had a better understanding of what you are trying to do, this would probably be much cleaner - I do not advocate production use of this example:
Output: