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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:12:31+00:00 2026-05-26T18:12:31+00:00

I have an application which communicates with a database. Each table has a unique

  • 0

I have an application which communicates with a database. Each table has a unique id.

When I pull in a row from the database, I populate a class with all of the fields (this is all generated code).

I originally just used Int32 as the type of the ID, but kept finding that inappropriate types were being passed to methods etc.

My solution to this was to create a specific type which I call idUser (where User is the actual table name). The reason for this is that if I try to pass an idSalesMan into a method which is expecting an idUser, it generates a compile-time error, which is really useful and stops me from making school-boy errors.

The code behind the idUser is like this..

[Serializable]
public struct idUser : IComparable
{
    public idUser(int InitVal) { this.m_Main=InitVal; }
    private int m_Main;
    public int Main { get { return m_Main; } set { m_Main = value; }}
    public static implicit operator int(idUser Main) { return Main.m_Main; }
    public static implicit operator idUser(int Main) { return new idUser(Main); }
    public static bool operator ==(idUser one, idUser two) { return one.m_Main == two.m_Main; }
    public static bool operator !=(idUser one, idUser two) { return one.m_Main != two.m_Main; }
    public static bool operator <(idUser one, idUser two) { return one.m_Main < two.m_Main; }
    public static bool operator >(idUser one, idUser two) { return one.m_Main > two.m_Main; }
    public override string ToString() {{ return this.m_Main.ToString(CultureInfo.InvariantCulture); }}
    public override bool Equals(object obj) { return this.m_Main == ((idUser)obj).m_Main; }
    public override int GetHashCode() { return this.m_Main.GetHashCode(); }
    public int CompareTo(object obj) { return this.m_Main.CompareTo(((idUser)obj).m_Main); }
}

This works fine, is easy (as it’s generated by a tool I’ve written), and prevents the issues I present above.

However, as my database has about 100 tables it generates a lot of code, and quite a large executable. (this isn’t a major issue).

I was curious as to the performance impact of using the above structure rather than just a standard Int32, and I was quite shocked.

Creating a List<> populating it, then checking for the existance of some of the keys..

When using List<idUser> it takes 3 times longer to populate it, and 5 times longer to check some of the added items.

This is quite a major hit, so I was looking at alternative ways of doing this.

Ideally, I’d like to be able to say…

public struct idUser : Int32 { }
public struct idCustomer : Int32 { }

But that obviously doesn’t work.

As I said above, it works fine as it is, but just has a performance hit. Does anyone have any ideas about how to incorporate the strict type-checking, whilst retaining the performance of the Int32 ?

Thanks.

  • 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-26T18:12:32+00:00Added an answer on May 26, 2026 at 6:12 pm

    I suspect I can explain the performance problem with finding values in a List<T> – you aren’t implementing IEquatable<T>, so any Equals call has to box the value it’s comparing with. You should also implement IComparable<T> for comparisons.

    // Please don't use camelCased type names...
    // I'd probably use UserId, CustomerId etc, partly to avoid non-interfaces
    // starting with I.
    public struct IdUser : IComparable, IComparable<IdUser>, IEquatable<IdUser>
    {
       ...
       public bool Equals(IdUser other) { return m_Main == other.m_Main; }
       public int CompareTo(IdUser other) { return m_Main.CompareTo(other.mMain); }
    }
    

    You may well find that just this change brings the performance to an acceptable level. It won’t help when populating the list, admittedly – but it’ll depend on what your required operations are.

    Note that in most cases I’d expect the bulk of the performance hit to be transferring data from the database to start with; comparing the performance of populating a list just with in-memory data isn’t really a realistic test of the performance impact on your real application.

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

Sidebar

Related Questions

I have an application which extracts data from an XML file using XPath. If
I have an application which behaves as a slideshow for all pictures in a
I have an application which takes a string value of the form %programfiles%\directory\tool.exe from
I have an application which has to live as a service, I create an
I have an application for Android which communicates with a Google App Engine (GAE)
I am developing an application which communicates with an SQL Server 2005 database to
I have a desktop and web application connected to same database. Which is the
I have a Spring MVC application which communicates with the frontend with AJAX /
I have a WPF application that directly communicates with a MS SQL 2008 database.
I have a database which holds URL's in a table (along with other many

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.