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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:57:06+00:00 2026-05-29T22:57:06+00:00

The specific exception I’m getting is: Unable to cast object of type NbtByte to

  • 0

The specific exception I’m getting is:

Unable to cast object of type NbtByte to type INbtTag<System.Object>

On this line:

tag = (INbtTag<object>)new NbtByte(stream);

Where tag is declared as:

INbtTag<object> tag;

And NbtByte is defined as:

public class NbtByte : INbtTag<byte>

Where IBtTag is:

public interface INbtTag<out T>

I thought by declaring it as out T I would be able to do things like this.

Basically, I want to have a dictionary of IbtTag<T>s,

var dict = new Dictionary<string, INbtTag<object>>();

but where T is of different types (thus I declared it with object). Is this possible?

  • 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-29T22:57:08+00:00Added an answer on May 29, 2026 at 10:57 pm

    Interface variance applies only to reference types. Value types (such as ints, bytes, etc., as well as custom structs) are excluded. For example, you cannot use an array of integers as IEnumerable<object> even though the array is IEnumerable<int>.

    IEnumerable<object> objs = new int[] { 1, 2, 3 }; // illegal
    IEnumerable<object> objs = new string[] { "a", "b", "c" }; // legal
    

    To solve your issue with the dictionary, you can perhaps elect to define a non-generic interface. (Where your generic interface might expose members as type T, the non-generic interface would simply expose object.)

    Say you have

    interface INbtTag { } // non-generic interface 
    interface INbtTag<out T> : INbtTag { } // covariant generic interface
    

    Then you could use your dictionary as Dictionary<string, INbtTag>.

    The drawback is that when you implement the interface, you have to implement both. This usually means implementing the generic version implicitly, and the non-generic explicitly. For example:

    interface INbtTag
    {
        object GetValue(); 
    }
    
    interface INbtTag<out T> : INbtTag
    {
        T GetValue();
    }
    
    class NbtByte : INbtTag<byte>
    {
        byte value;
    
        public byte GetValue() // implicit implementation of generic interface
        {
            return value;
        }
    
        object INbtTag.GetValue() // explicit implementation of non-generic interface
        {
            return this.GetValue(); // delegates to method above
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting exception: Specific cast is not valid, here is the code con.Open();
When I run the code for this specific value of dt, an exception is
I would like to ignore a specific Type of exception in a group of
When catching an exception in .NET, you can have as many type-specific exception blocks
There are situations that we like to ignore a specific exception type (commonly ObjectDisposedException
I've looked at the various questions that mention this specific exception ( this question
I'm getting the following unhandled exception in a specific case and I can't seem
I have some code which ignores a specific exception. try { foreach (FileInfo fi
Is there any way to direct C# to ignore NullReferenceException (or any specific exception
I've created a custom exception for a very specific problem that can go wrong.

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.