Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5957801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:27:11+00:00 2026-05-22T18:27:11+00:00

I am working a with a geo-coding API and need to represent the coordinate

  • 0

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:

Interface

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

LatLng Class Implementation

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));
    }
}

LatLng Struct Implementation

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 = new SLatLng(); s = null;

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

  • 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?

  • 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?

  • 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:27:12+00:00Added an answer on May 22, 2026 at 6:27 pm

    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);
        }
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working with viewports and bounds returned from Google Geocoding API . When
I am working on one iPhone app which needs latitude and longitude from address.
I am working on a simple app engine application that utilize the geo-searching proximity_fetch.
I'm trying to get GeoIP working with hive. I found this: http://www.jointhegrid.com/hive-udf-geo-ip-jtg/index.jsp , which
I'm working on a product which heavily relies on Geo-Data to map locations of
I'm working on a Geo/Spatial search where I'm looking for nearby points. I have
I have two classes and I want to pass a (geo-point) variable on touch
I am working with the Twitter API, accessing it via C# and DataContracts. I
I am working some portion of twitter api, and getting some JSON response using
I am currently working on google map api V3 using cakephp with prototype.js. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.