创造你自己的“int”或“字符串”班级
我反汇编了 .NET“系统”DLL,并查看了变量类(字符串、整数、字节等)的源代码,看看是否可以弄清楚如何创建一个可以接受值的类。我注意到“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());
}
I disassembled the .NET 'System' DLL and looked at the source code for the variable classes (string, int, byte, etc.) to see if I could figure out how to make a class that could take on a value. I noticed that the "Int32" class inherits the following: IComparable, IFormattable, IConvertible, IComparable, IEquatable.
The String and Int32 classes are not inheritable, and I can't figure out what in these inherited interfaces allows the classes to hold a value. What I would want is something like this:
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());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过重载运算符来做到这一点。 MSDN 上有一个教程。
假设您想要一个可以是字符串或整数的类型(例如 Haskell 的 两者之一):
You can do that by overloading operators. There is a tutorial on MSDN.
Let's say you want a type that can be either a string or an int (like Haskell's Either):
C# 中的内置类型被“特殊”处理,因为代码中的文字值
1234
被定义为 System.Int32 类型,而文字值“some string”
被定义为 System.Int32 类型。 code> 被定义为 System.String 类型。因此,为了支持您想要的代码类型,您需要提供可以从 int 和 string(以及其他任何类型)转换为您的类型的转换运算符。查看 MSDN 中的转换运算符主题。
The built-in types in C# are handled "specially" in that the literal value
1234
in code is defined as being of type System.Int32, and the literal value"some string"
is defined as being of type System.String.So in order to support the kind of code you want, you're going to need to provide conversion operators that can convert from int and string (and whatever else) to your type. Have a look at the conversion operators topic in MSDN.
通常,您希望创建一个新类型来存储新的数据表示形式。在上面的示例中,您使用 MyVariable 来存储字符串 - 字符串类型已经擅长这样做。
如果您想存储不同类型的数据,例如在一个易于发送的包中存储字符串和 int 的组合,您可以按照上面开始的方式进行操作:
然后:
IComparable 和 IFormattable 等接口允许您的新类型以特定方式使用,例如 IComparable 可以传递到排序列表,因为它能够将其自身与另一个实例进行比较并相应地对它们进行“排名”。
Generally you want to make a new type to store a new representation of data. In your example above you are using MyVariable to store strings - something the string type is already good at doing.
If you want to store a different type of data, such as a combination of string and int in one easy to send around package you would do it as you've started above:
Then:
Interfaces such as IComparable and IFormattable allow your new type of be used in specific ways, for instance IComparable can be passed to sorted lists as it is capable of comparing its self to another instance and 'ranking' them accordingly.
“int”和“string”是内置类型。如果没有可辨别的领域,你将无法制作出类似的产品。但是,您可以创建一些外观和行为几乎与内置类型完全相同的东西。
最好的例子是
Nullable
。这提供了所有值类型的可为空版本,并且可以像内置一样进行分配:其工作方式是
Nullable
覆盖显式和隐式转换。通过这种方式,您可以将 int 等内置类型分配给自定义Nullable
,并且Nullable.op_Implicit
中的代码会透明地处理转换。这是一个完整的示例:
"int" and "string" are built-in types. You won't be able to make one just like it that has no discernible fields. However, you can make something that looks and behaves almost exactly like a built-in type.
The best example is
Nullable<T>
. This provides a nullable version of all value types and can be assigned just like it's built in:The way this works is
Nullable<T>
overrides the explicit and implicit casts. This way you can assign a built-in type like int to a customNullable<int>
and code withinNullable<int>.op_Implicit
handles the conversion transparently.Here's a complete example: