【发布时间】:2010-04-06 01:48:31
【问题描述】:
我反汇编了 .NET 'System' DLL 并查看了变量类(字符串、int、字节等)的源代码,看看我是否能弄清楚如何创建一个可以接受值的类.我注意到“Int32”类继承了以下内容:IComparable、IFormattable、IConvertible、IComparable、IEquatable。
String 和 Int32 类是不可继承的,我不知道在这些继承的接口中什么允许类保存值。我想要的是这样的:
public class MyVariable : //inherits here
{
//Code in here that allows it to get/set the value
}
public static class Main(string[] args)
{
MyVariable a = "This is my own custom variable!";
MyVariable b = 2976;
if(a == "Hello") { }
if(b = 10) { }
Console.WriteLine(a.ToString());
Console.WriteLine(a.ToString());
}
【问题讨论】:
-
我可以编译,但是...为什么?
-
另外,第二个 if 缺少
=(应该是b == 10) -
在上面代码的第12行,应该是
if(b == 10) {}而不是if(b = 10) {}? -
抱歉出现错误,它不是用来编译的。只是一个简单的例子,展示了我想要它做什么。我在底部附近找到了答案(重载运算符)。
-
既然你说的答案在 cmets 中是正确的,你应该接受一个作为正确答案(点击复选标记)。
标签: .net string variables int inheritance