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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:57:10+00:00 2026-06-04T07:57:10+00:00

I used an interface and class similar to following: public interface IIdentity { int

  • 0

I used an interface and class similar to following:

public interface IIdentity
{
    int Id { get; set; }
}

public class Identity : IIdentity
{
    public int Id { get; set; }
}

I was creating instances of the Identity class and adding it to List<Identity> (later referred to as instances creation block).

var identities = new List<IIdentity>();
identities.Add( new Identity { Id = 1 } );
identities.Add( new Identity { Id = 2 } );
identities.Add( new Identity { Id = 3 } );

and then using the identities as follows:

foreach ( IIdentity identity in identities )
{
    Console.WriteLine( "Plug-in: {0}", identity.Id.ToString() );
}

Recently I needed to add more data about IIdentity instances without making any modification to IIdentity and Identity. Therefore I added following class:

public class Wrapper<T> where T : class
{
    public T WrappedObject { get; set; }
    public string Name { get; set; }
    public int Order { get; set; }
}

and replaced the instances creation block with following:

var identities = new List<Wrapper<IIdentity>>();
identities.Add( new Wrapper<IIdentity> { WrappedObject = new Identity { Id = 1 }, Name = "John", Order = 1 } );
identities.Add( new Wrapper<IIdentity> { WrappedObject = new Identity { Id = 2 }, Name = "Jane", Order = 2 } );
identities.Add( new Wrapper<IIdentity> { WrappedObject = new Identity { Id = 3 }, Name = "Joe", Order = 3 } );

I was expecting that I would still have to make some modification to foreach block to make the application compile. However, application compiled successfully and threw System.InvalidCastException when it was run.

As it is visible from provided code, Wrapper does not implement IIdentity interface.

However, the compiler was complaining if one of two modifications was made:

  1. Wrapper class is sealed.
  2. Line 1 is replaced with line 2

Line 1:

foreach ( IIdentity identity in identities )

Line 2:

foreach ( var identity in identities )

The question is why did compiler successfully compile application when modification 1 or 2 are not in place?

  • 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-04T07:57:12+00:00Added an answer on June 4, 2026 at 7:57 am

    The compiler sees that the type of identities is List<Wrapper<IIdentity>>, and it can see that Wrapper<T> does not implement IIdentity. However, by itself that’s not enough to produce a compile-time error because there might be a derived class like this somewhere:

    class DerivedWrapper<T> : Wrapper<T>, IIdentity { ... }
    

    Instances of DerivedWrapper can legitimately be put inside identities, so the compiler has to try to cast identity to IIdentity at runtime and throw an exception if this fails.

    The two modifications affect this in different ways:

    1. If Wrapper is sealed then the compiler knows there can be no such derived class so the runtime cast can never succeed; helpfully, it promotes this to a compile time error.

    2. If implicit typing with var is used then the compiler resolves var to the static type of identity, which in this instance is Wrapper<IIdentity>. This means that the loop body tries to access the non-existent member Wrapper<IIdentity>.Id member, hence a compile-time error.

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

Sidebar

Related Questions

Since I'm a C#-/.NET-guy, I'm used to explicit interface implementations - like so: public
I've got an interface which i've used StructureMap to Dependency Inject . public interface
In C++, you can do the following: class base_class { public: virtual void do_something()
Consider the following code: public abstract class RepositoryBase<T> where T : class { #region
I have two Java interfaces and one implementing class. (I have used Eclipse to
I used the Interface Builder to create a table view, to which I added
For example: @interface Fraction: NSObject { ... When wouldn't NSObject be used and is
I've never come across this extension before. It's being used for a front-end interface
I am about to add a class X that will be used by my
I've got the following code, think simple shooter in c++: // world.hpp //---------- 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.