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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:33:28+00:00 2026-06-18T12:33:28+00:00

In C# I have a generic class: public class MyGeneric<ParameterClass> where ParameterClass: MyGenericParameterClass, new()

  • 0

In C# I have a generic class:

public class MyGeneric<ParameterClass> where ParameterClass: MyGenericParameterClass, new() {
    public static int Variable;
}

Now in C++ if I instantiated a templated class with different parameters each complete class would get it’s own Variable, so I just can’t say

MyGeneric.Variable = 1; // invalid in C++

in C++, but seems like I can do so in C#.

I’d like to clarify…

If I have a generic with a static member variable is that variable shared among all generic instantiations?

  • 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-06-18T12:33:29+00:00Added an answer on June 18, 2026 at 12:33 pm

    Section 25.1.4 of the ECMA C# Language specification

    A static variable in a generic class declaration is shared amongst
    all instances of the same closed constructed type (§26.5.2), but is
    not shared amongst instances of different closed constructed types.

    These rules apply regardless of whether the type of the static
    variable involves any type parameters or not.

    You may see this blog post: Static fields in generic classes by Gus Perez

    You can’t do that in C# as well.

    MyGeneric.Variable = 1;
    

    Consider the following example from ECMA Language Specification.

    class C<V>
    {
        static int count = 0;
        public C()
        {
            count++;
        }
        public static int Count
        {
            get { return count; }
        }
    }
    class Application
    {
        static void Main()
        {
            C<int> x1 = new C<int>();
            Console.WriteLine(C<int>.Count);  // Prints 1 
            C<double> x2 = new C<double>();
            Console.WriteLine(C<double>.Count); // Prints 1 
            Console.WriteLine(C<int>.Count);  // Prints 1 
            C<int> x3 = new C<int>();
            Console.WriteLine(C<int>.Count);  // Prints 2 
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a generic class public class Decoder<SIGNAL> where SIGNAL : signalType, new() signalType
Let's say I have a generic class: public class MyGenericClass<T> { ... } Now
I have generic static class: public static class IsolatedStorageCacheManager<BagType> { } and method: public
I have a generic base class: public class BaseLabel<T> extends JXLabel { private static
I have the following code implementation of my generic singleton provider: public sealed class
I have the following generic class: public class MessageProcesser<T> where T : Message Inside
I have a generic class NamedValue<TValue> : public class NamedValue<TValue> { public string Name
If say I have some generic class, for example: public class Attribute<T> { }
I have generic type that looks like: public class GenericClass<T, U> where T :
I have the following generic repository: public class GenericRepository<TEntity> where TEntity : class {

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.