我应该使用一个结构或类重新present一个纬度/经度坐标? [英] Should I use a struct or a class to represent a Lat/Lng coordinate?

查看:204
本文介绍了我应该使用一个结构或类重新present一个纬度/经度坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个一个地理编码的API,并需要重新present返回的点作为纬度/经度对的坐标。不过,我不能确定是否使用一个结构或类此。我最初的想法是使用一个结构,但他们似乎普遍在C#中皱着眉头(例如,乔恩斯基特提到的说>我的几乎的从未定义自定义结构)。性能和内存使用情况的应用程序并不重要因素。

I am working a with a geo-coding API and need to represent the coordinate of a returned point as a Latitude / Longitude pair. However, I am unsure whether to use a struct or a class for this. My initial thought was to use a struct, but they seem to be generally frowned upon in C# (for instance, Jon Skeet mentions in this answer that, "I almost never define custom structs"). Performance and memory usage are not critical factors in the application.

到目前为止,我已经提出了基于一个简单的界面,这两种实现方式:

So far I have come up with these two implementations based on a simple interface:

接口

public interface ILatLng
{
    double Lat { get; }
    double Lng { get; }
}

经纬度类实现

public class CLatLng : ILatLng
{
    public double Lat { get; private set; }
    public double Lng { get; private set; }

    public CLatLng(double lat, double lng)
    {
        this.Lat = lat;
        this.Lng = lng;
    }

    public override string ToString()
    {
        return String.Format("{0},{1}", this.Lat, this.Lng);
    }

    public override bool Equals(Object obj)
    {
        if (obj == null)
            return false;

        CLatLng latlng = obj as CLatLng;
        if ((Object)latlng == null)
            return false;

        return (this.Lat == latlng.Lat) && (this.Lng == latlng.Lng);
    }

    public bool Equals(CLatLng latlng)
    {
        if ((object)latlng == null)
            return false;

        return (this.Lat == latlng.Lat) && (this.Lng == latlng.Lng);
    }


    public override int GetHashCode()
    {
        return (int)Math.Sqrt(Math.Pow(this.Lat, 2) * Math.Pow(this.Lng, 2));
    }
}

经纬度结构体实施

public struct SLatLng : ILatLng
{
    private double _lat;
    private double _lng;

    public double Lat
    {
        get { return _lat; }
        set { _lat = value; }
    }

    public double Lng
    {
        get { return _lng; }
        set { _lng = value; }
    }

    public SLatLng(double lat, double lng)
    {
        this._lat = lat;
        this._lng = lng;
    }

    public override string ToString()
    {
        return String.Format("{0},{1}", this.Lat, this.Lng);
    }
}

执行一些测试,我得出以下结论:

Performing some tests I've come to the following findings:

  • 一个结构总是有一个参数的构造函数,这意味着你不能强迫它有一个构造函数,需要两个属性(纬度和经度)被实例化,你可以用一个类。

  • A struct always has a parameterless constructor, which means you can't force it to be instantiated with a constructor which expects two properties (for lat and lng), as you can with a class.

一个结构(该值类型)不能为空,所以总是会包含一个值。但是,你仍然可以做这样的东西,如果实现一个接口:

A struct (being a value type) can never be null, so will always contain a value. But you can still do stuff like this if implementing an interface:

ILatLng S =新SLatLng(); S = NULL;

这是否有意义的结构使用一个接口在这种情况下?

So does it make sense for a struct to use an interface in this case?

  • 如果我用一个结构,我需要重写等于 GetHash code()等?我的测试表明比较正常工作没有这样做(不像一类) - 所以有必要

  • If I use a struct do I need to override Equals, GetHashCode() etc. ? My tests indicate comparisons work correctly without doing so (unlike with a class) - so is it necessary?

我觉得更舒适使用类,所以它最好还是跟他们坚持为我更知道他们如何表现的?会用我的code人民的价值型语义,尤其是工作到一个接口时,?混淆

I feel more 'comfortable' using classes, so is it best to just stick with them as I'm more aware of how they behave? Will people using my code be confused by value-type semantics, especially when working to an interface?

CLatLng 的实施,确实对 GetHash code()覆盖似乎确定?我从这篇文章'偷'了,所以我不能确定!

In the CLatLng implementation, does the override of GetHashCode() seem OK? I 'stole' it from this article, so am unsure!

任何帮助或建议感激地接受!

Any help or advice gratefully received!

推荐答案

我看不到任何一点有这个接口,是诚实的。

I can't see any point in having an interface for this, to be honest.

我只想创造一个结构,但要不变 - 可变的结构是一个非常糟糕的主意。我也想使用完整的天气预报经度的属性名称。事情是这样的:

I would just create a struct, but make it immutable - mutable structs are a really bad idea. I'd also use full Latitude and Longitude as the property names. Something like this:

public struct GeoCoordinate
{
    private readonly double latitude;
    private readonly double longitude;

    public double Latitude { get { return latitude; } }
    public double Longitude { get { return longitude; } }

    public GeoCoordinate(double latitude, double longitude)
    {
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public override string ToString()
    {
        return string.Format("{0},{1}", Latitude, Longitude);
    }
}

我会那么的的实施 IEquatable< GeoCoordinate> 并覆盖等于 GetHash code ,例如:

I'd then also implement IEquatable<GeoCoordinate> and override Equals and GetHashCode, e.g.

public override bool Equals(Object other)
{
    return other is GeoCoordinate && Equals((GeoCoordinate) other);
}

public bool Equals(GeoCoordinate other)
{
    return Latitude == other.Latitude && Longitude == other.Longitude;
}

public override int GetHashCode()
{
    return Latitude.GetHashCode() ^ Longitude.GetHashCode();
}

请注意,你需要知道对双打进行平等的比较正常的危险 - 没有太多另类这里,但两个值其中的的像他们应该是平等的可能不...

Note that you need to be aware of the normal dangers of performing equality comparisons on doubles - there's not much alternative here, but two values which look like they should be equal may not be...

有关参数的构造函数问题的关键是合理的,但我怀疑你会发现它不会的实际上的咬你。

The point about the parameterless constructor is a reasonable one, but I suspect you'll find it won't actually bite you.

这篇关于我应该使用一个结构或类重新present一个纬度/经度坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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