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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:12:11+00:00 2026-05-30T10:12:11+00:00

I have an interface for a base class, and every class that inherits from

  • 0

I have an interface for a base class, and every class that inherits from the base class should have an identifying field which tells the application what kind of object it is.

I wanted to use this property in two different ways:

Without creating an instance of the object

if (someValue == TestA.Id)
    return new TestA();

elseif (someValue == TestB.Id)
    return new TestB();

And as a property of the interface

void DoSomething(ITest testObject)
{
    SomeValue = testObject.Id;
}

Is there an easy way to define the Id field in the interface, but still have it available to use without creating an instance of the class?

Right now I am using the following code. I could add a read-only Id property to the interface which returns the const string, however I was hoping there was a simpler way that I’m just not aware of.

public interface ITest
{

}

public class TestA : ITest
{
    public const string Id = "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-30T10:12:13+00:00Added an answer on May 30, 2026 at 10:12 am

    In short – no.

    In order to be able to do this, you’d need to be able to specify this as a instance property on the interface (and implement it in the instance), and as a static property on the type.

    The compiler won’t let you do this.

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

Sidebar

Related Questions

I have an abstract base class which acts as an interface. I have two
I have a base class and classes that derive from it. The base class
I have an interface with several events I have base class implementing the interface
I'd like to find all the types inheriting from a base/interface. Anyone have a
I have an abstract immutable base class that defines enforces child classes to be
If I have a method that has as argument a Base class type I
I want to test which interfaces a class/interface is implementing. So I have a
Possible Duplicates: Interface vs Base class When should I choose inheritance over an interface
I have an abstract generic view-model that I use as a base-class for several
I'm making a chess game, and I have a base 'piece' interface that I

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.