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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:40:43+00:00 2026-05-13T08:40:43+00:00

I ran across the following class in a C# XNA graphics api and I

  • 0

I ran across the following class in a C# XNA graphics api and I am not sure what it does or that it needs to be so obscure. (T is constrained to be a struct in a parent class)

    static class Ident
    {
        static object sync = new object();
        static volatile int index = 0;
        static int Index
        {
            get
            {
                lock (sync)
                    return index++;
            }
        }
        class Type<T>
        {
            public static int id = Index;
        }
        public static int TypeIndex<T>()
        {
            return Type<T>.id;
        }
    }

The API makes only on call to this static class: int index = Ident.TypeIndex<T>();

  • 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-13T08:40:43+00:00Added an answer on May 13, 2026 at 8:40 am

    This creates a unique integer identifier for each Type, based on the order that it’s accessed, in a thread-safe manner.

    For example, if you do:

    int myClassId = Ident.TypeIndex<MyClass>();
    int mySecondClssId = Ident.TypeIndex<MySecondClass>();
    

    You’ll get 2 “TypeIndex” numbers (with mySecondClassId being at least 1 more than myClassId, but potentially greater, due to threading). Later, if you call this again with the same class, it will return the same TypeIndex for that class.

    For example, if I run this, using:

    Console.WriteLine(Ident.TypeIndex<Program>());
    Console.WriteLine(Ident.TypeIndex<Test>());
    Console.WriteLine(Ident.TypeIndex<Program>());
    Console.WriteLine(Ident.TypeIndex<Test>());
    

    It will print:

    0
    1
    0
    1
    

    However, this could be done more effectively using Interlocked.Increment, which would avoid the need for the lock and the synchronization object completely. The following gives exactly the same answer, with no locking required:

    static class Ident
    {
        static int index = -1;
        static int Index
        {
            get
            {
    
                return Interlocked.Increment(ref index);
            }
        }
        private static class Type<T>
        {
            public static int id = Index;
        }
    
        public static int TypeIndex<T>()
        {
            return Type<T>.id;
        }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently ran across a class that had the following field declared: private final
I ran across a tutorial that had the following syntax: rails generate model User
I recently ran across some 3rd party C# code which does the following: public
I ran across the following in a stored procedure: for select 1 from scan_queue
I ran across the following code in one of our in-house dlls and I
I ran across a compilation issue today that baffled me. Consider these two container
I ran across an interesting issue today. We have an application that utilizes Zend
I ran across a comment that made me wonder: If you use a sharding
I ran across some very interesting code that makes me wonder about what bool
I just ran across the following syntax in a piece of Haskell code -

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.