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 4080754
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:58:14+00:00 2026-05-20T17:58:14+00:00

Is there a way to strongly type integer ID values in C#? I’ve recently

  • 0

Is there a way to strongly type integer ID values in C#?

I’ve recently been playing with Haskell and can immediately see the advantages of its strong typing when applied to ID values, for example you would never want to use a PersonId in place of a ProductId.

Is there a nice way to create an Id class/struct that can be used to represent IDs of a given type?

I had the following idea but unfortunately it isn’t legal on many levels. You can’t have an abstract struct and the implicit/explicit cast operators wouldn’t be inherited.

public abstract struct Id
{
    int _value;

   public Id(int value)
   {
      _value = value;
   }

   // define implicit Id to int conversion operator:
   public static implicit operator int(Id id) 
   {
      return _value;    
   }

   // define explicit int to Id conversion operator:
   public static explicit operator Id(int value) 
   {
      return new Id(value);
   }

   public bool Equals(object obj)
   {
      if(GetType() == obj.GetType()) 
      {
         Id other = (Id)obj;
         return other._value == _value;
      }
      return false;
   }

   public int GetHashCode()
   {
      return _value.GetHashCode();
   }
}

struct PersonId : Id { public PersonId(int value) : base(value) {} }
struct ProductId : Id { public ProductId(int value) : base(value) {} }

Are there any valid ways to perform something similar? How else can we prove that integer IDs type aren’t being confused across a large application?

  • 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-20T17:58:14+00:00Added an answer on May 20, 2026 at 5:58 pm
    public interface IId { }
    
    public struct Id<T>: IId {
        private readonly int _value;
    
        public Id(int value) {
            this._value = value;
        }
    
        public static explicit operator int(Id<T> id) {
            return id._value;
        }
    
        public static explicit operator Id<T>(int value) {
            return new Id<T>(value);
        }
    }
    
    public struct Person { }  // Dummy type for person identifiers: Id<Person>
    public struct Product { } // Dummy type for product identifiers: Id<Product>
    

    Now you can use types Id<Person> and Id<Product>. The Person and Product types can be either structs or classes. You can even use the actual types that are identified by the id and in that case you do not need any dummy types.

    public sealed class Person {
        private readonly Id<Person> _id;
        private readonly string _lastName;
        private readonly string _firstName;
    
        // rest of the implementation...
    }
    

    The explicit operator overloads allow safe and easy casting between id types and underlying id values. When working with legacy interfaces you may want to change the casting to integer to be implicit, or even better, to overload the legacy interfaces with properly typed versions. Extension methods can be used when the legacy interface is from a third party and cannot be changed or overloaded directly.

    public interface ILegacy {
        public bool Remove(int user);
    }
    
    public static class LegacyExtensions {
        public static bool Remove(this ILegacy @this, Id<Person> user) {
            return @this.Remove((int)user);
        }
    }
    

    Edit: Added IId interface as suggested by smartcaveman.

    Edit: Changed both operators to be explicit after thinking about Alejandro’s suggestion and added a section how to deal with legacy interfaces.

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

Sidebar

Related Questions

is there way thats i can preselect an item when the page loads or
Is there a way to find the number of files of a specific type
Is there way in next piece of code to only get the first record?
Is there a way to enforce constraint checking in MSSQL only when inserting new
Is there any way to check whether a file is locked without using a
Is there a way to find the name of the program that is running
Is there a way to hide radio buttons inside a RadioButtonList control programmatically?
Is there a way to take over the Entity Framework class builder? I want
Is there any way to capture the MouseDown even from the .NET 2.0 TextBox
Is there a way to identify, from within a VM, that your code is

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.