在C#中的新类型定义 [英] New type definition in C#

查看:400
本文介绍了在C#中的新类型定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找的可能性来定义一个新的类型,在C#中使用它象下面这样:



类定义:

 公共类职位
{
公共double180经度{搞定;组; } // double180是一个范围-180和180
公共double90纬度中的一个类型{搞定;组; } // double90是一个范围介于-90和90
}



用法中的一个类型

  VAR位置=新的位置
{
经度= 45,
纬度= 96 //当初始化对象
}这行应该给出一个错误;


解决方案

一个类型可以是矫枉过正,但如果你想要一个,这是一个良好的开端:

 结构Double180:IEquatable< Double180> 
{
私人只读double值;如果

公共Double180(双D)
{
(D< -180 || D> 180)
{
抛出新ArgumentOutOfRangeException( D);
}

THIS.VALUE = D;
}

公共静态隐运营商双(Double180 D)
{
返回d.value;
}

公共静态明确经营者Double180(双D)
{
返回新Double180(D);
}

公共重写字符串的ToString()
{
返回this.value.ToString();
}

公共布尔等于(Double180等)
{
返回THIS.VALUE == other.value;
}

公众覆盖布尔等于(obj对象)
{
返回obj是Double180&放大器;&安培; this.Equals((Double180)OBJ);
}

公共覆盖INT的GetHashCode()
{
返回this.value.GetHashCode();
}

公共静态布尔运算符==(Double180一,Double180 B)
{
返回a.Equals(B);
}

公共静态布尔运算符=(Double180一,Double180 B)
{
返回a.Equals(B);!
}
}



当然,还有更多的实现接口,例如 IConvertible IComparable的< Double180方式> 将是不错



正如你所看到的,你知道这个开始,但你不知道它在哪里结束。



一个二传手验证,由其他答案的建议,可能是一个更好的主意。


I am looking for possibilities to define a new type and using it in C# like below:

Class definition:

public class Position
{
    public double180 Longitude { get; set; } // double180 is a type within a range -180 and 180
    public double90 Latitude { get; set; } // double90 is a type within a range of -90 and 90
}

Usage:

var position = new Position
{
     Longitude = 45,
     Latitude = 96 // This line should give an error while initializing the object
};

解决方案

A type may be overkill, but if you want one, this is a good start:

struct Double180 : IEquatable<Double180>
{
    private readonly double value;

    public Double180(double d)
    {
        if (d < -180 || d > 180)
        {
            throw new ArgumentOutOfRangeException("d");
        }

        this.value = d;
    }

    public static implicit operator double(Double180 d)
    {
        return d.value;
    }

    public static explicit operator Double180(double d)
    {
        return new Double180(d);
    }

    public override string ToString()
    {
        return this.value.ToString();
    }

    public bool Equals(Double180 other)
    {
        return this.value == other.value;
    }

    public override bool Equals(object obj)
    {
        return obj is Double180 && this.Equals((Double180)obj);
    }

    public override int GetHashCode()
    {
        return this.value.GetHashCode();
    }

    public static bool operator ==(Double180 a, Double180 b)
    {
        return a.Equals(b);
    }

    public static bool operator !=(Double180 a, Double180 b)
    {
        return !a.Equals(b);
    }
}

Of course, there are many more interfaces to implement, for example IConvertible and IComparable<Double180> would be nice.

As you can see, you know where this starts, but you don't know where it ends.

A setter validator, as suggested by the other answers, might be a better idea.

这篇关于在C#中的新类型定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆