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

  • Home
  • SEARCH
  • 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 1025621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:57:19+00:00 2026-05-16T11:57:19+00:00

I have a question regarding the following code: abstract class a { public static

  • 0

I have a question regarding the following code:

abstract class a
{
    public static string x;
}



class b<c> where c : a
{
    public void f()
    {
        c.x=10;
    }
}

This code does not compile. I get an error at the statement c.x=10; . The problem makes it look as if the condition where c:a does not have any effect at all.Can someone please explain why this is an error? Is it not true that x is shared as a static member by all children of a? And is there a way to circumvent this problem?

What I am trying to achieve is this : i have a subclass of a, all of whose objects share a common property and this property has to be set through f() in the generic class b. Is it alright if i replace the statement in question with a.x=10? If not, how is a.x different from c.x (or h.x where h is the subclass of a)?

  • 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-16T11:57:20+00:00Added an answer on May 16, 2026 at 11:57 am

    Static members are not inherited, although it’s confusingly possible to access a static member through a derived type. For example, in the following code

    class P
    {
        public static string X;
    }
    
    class Q : P { }
    
    class R : P { }
    

    you can access P.X through P.X or Q.X or R.X but it’s still the same field:

    P.X = "Hello";
    Q.X = "World";
    Console.WriteLine(R.X);  // prints "World"
    

    As you’ve discovered, you can’t do this with generic type parameters. But accessing X though the type parameter doesn’t really make a lot of sense, because all you change is P.X which you write directly without the generic type parameter.


    I’m not really sure what you’re trying to achieve. If you have an abstract class A and want all instances of types that derive from A to have a certain property, you can define this:

    abstract class A
    {
        public abstract string X
        {
            get;
        }
    }
    
    class A1 : A
    {
        public override string X
        {
            get { return "A1"; }
        }
    }
    
    class A2 : A
    {
        public override string X
        {
            get { return "A2"; }
        }
    }
    

    If you want to associate a bit of information with a type (not instance), you can define a static field that is parameterized with a type using a generic class:

    class Info<T>
    {
        public static string X;
    }
    
    Info<A1>.X = "Hello";
    Info<A2>.X = "World";
    
    Console.WriteLine(Info<A1>.X);  // prints "Hello"
    Console.WriteLine(Info<A2>.X);  // prints "World"
    

    What about this?

    abstract class Job
    {
        public abstract string ExePath
        {
            get;
        }
    
        public void Execute(string[] args)
        {
            Console.WriteLine("Executing {0}", this.ExePath);
        }
    }
    
    abstract class Job<T> where T : Job<T>
    {
        public override string ExePath
        {
            get { return JobInfo<T>.ExePath; }
        }
    }
    
    class ConcreteJob1 : Job<ConcreteJob1> { }
    
    class ConcreteJob2 : Job<ConcreteJob1> { }
    
    static class JobInfo<T> where T : Job<T>
    {
        public static string ExePath;
    }
    
    static class JobInfoInitializer
    {
        public static void InitializeExePaths()
        {
            JobInfo<ConcreteJob1>.ExePath = "calc.exe";
            JobInfo<ConcreteJob2>.ExePath = "notepad.exe";
        }
    }
    

    This matches closely the process you describe in your comment. It should work, although it’s not how I would design a configurable Job model.

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

Sidebar

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.