代码之家  ›  专栏  ›  技术社区  ›  afollestad

创建自己的“int”或“string”类

  •  2
  • afollestad  · 技术社区  · 16 年前

    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());
    }
    
    4 回复  |  直到 16 年前
        1
  •  7
  •   R. Martinho Fernandes    16 年前

    你可以通过 overloading operators . 有一个关于 MSDN .

    Either ):

    public sealed class StringOrInt32
    {
        private string stringValue;
        private int int32Value;
        private bool isString;
    
        public bool IsString { get { return isString; } }
        public bool IsInt32 { get { return !isString; } }
    
        public string StringValue
        {
            get
            {
                if(!isString) throw new InvalidOperationException();
                return stringValue;
            }
        }
    
        public int Int32Value
        {
            get
            {
                if(isString) throw new InvalidOperationException();
                return int32Value;
            }
        }
    
        public StringOrInt32(string value)
        {
            isString = true;
            stringValue = value;
        }
    
        public StringOrInt32(int value)
        {
            isString = false;
            int32Value = value;
        }
    
        // Allows writing this:
        // StringOrInt32 foo = "Hello world!";
        public static implicit operator StringOrInt32(string value)
        {
            return new MyVariable(value);
        }
    
        // Allows writing this:
        // StringOrInt32 foo = 42;
        public static implicit operator StringOrInt32(int value)
        {
            return new MyVariable(value);
        }
    
        // Allows writing this:
        // StringOrInt32 foo = "Hello world!;
        // string bar = (string)foo;
        // Though foo.StringValue directly would be better
        public static explicit operator string(StringOrInt32 value)
        {
            return value.StringValule;
        }
    
        // Allows writing this:
        // StringOrInt32 foo = 42;
        // int bar = (int)foo;
        // Though foo.Int32Value directly would be better
        public static explicit operator int(StringOrInt32 value)
        {
            return value.Int32Value;
        }
    
        public static bool operator==(StringOrInt32 left, StringOrInt32 right)
        {
            if(left.IsString != right.IsString)
                return false;
            if(left.IsString)
                return left.StringValue == right.StringValue;
            else
                return left.Int32Value == right.Int32Value;
        }
    
        public static bool operator!=(StringOrInt32 left, StringOrInt32 right)
        {
            return !(left == right)
        }
    
        // Don't forget to override object.Equals(), object.GetHashCode(),
        // and consider implementing IEquatable<StringOrInt32>
        // Also, null checks, etc
    }
    
        2
  •  3
  •   Dean Harding    16 年前

    1234 "some string"

    因此,为了支持所需的代码类型,您需要提供转换运算符,可以将int和string(以及其他任何类型)转换为您的类型。看一看这个 conversion operators MSDN中的主题。

        3
  •  1
  •   Michael Shimmins    16 年前

    如果您想在一个易于发送的包中存储不同类型的数据,例如字符串和int的组合,您可以按照上面的步骤进行:

    public class MyVariable
    {
        public string Name { get; set; }
        public int Age { get; set; }
    } 
    

    然后:

    MyVariable personOne = new MyVariable { Name = "John", Age = 34 };
    MyVariable personTwo = new MyVariable { Name = "Joe", Age = 312 };
    

    像IComparable和IFormattable这样的接口允许您以特定的方式使用新类型的实例,例如IComparable可以传递给排序列表,因为它能够将自己与另一个实例进行比较,并相应地对它们进行“排序”。

        4
  •  1
  •   Samuel Neff    16 年前

    最好的例子是 Nullable<T> . 这提供了所有值类型的可空版本,可以像内置的那样进行赋值:

    int? x = 0;
    int? y = null;
    

    这样做的方式是 可为空<T> Nullable<int> 和代码 Nullable<int>.op_Implicit 透明地处理转换。

    下面是一个完整的示例:

    public struct MyVariable
    {
        private int _intValue;
        private string _stringValue;
    
        public override bool Equals(object obj)
        {
            if (!(obj is MyVariable))
            {
                return false;
            }
            var y = (MyVariable) obj;
            return _stringValue == y._stringValue && _intValue == y._intValue;
        }
    
        public override int GetHashCode()
        {
            return (_stringValue ?? _intValue.ToString()).GetHashCode();
        }
    
        public override string ToString()
        {
            return _stringValue ?? _intValue.ToString();
        }
    
        public static implicit operator MyVariable(string value)
        {
            return new MyVariable { _stringValue = value };
        }
    
        public static implicit operator MyVariable(int value)
        {
            return new MyVariable { _intValue = value };
        }
    
        public static bool operator==(MyVariable variable, string value)
        {
            return variable._stringValue == value;
        }
    
        public static bool operator ==(MyVariable variable, int value)
        {
            return variable._intValue == value;
        }
    
        public static bool operator !=(MyVariable variable, string value)
        {
            return variable._stringValue == value;
        }
    
        public static bool operator !=(MyVariable variable, int value)
        {
            return variable._intValue == value;
        }
    
        public static void Test()
        {
            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());
        }
    }