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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:20:25+00:00 2026-05-27T07:20:25+00:00

I have a struct that implements some interface . This works fine until I

  • 0

I have a struct that implements some interface. This works fine until I have an array of the struct implementation and try to implicitly cast that array to another array of the interface type. (See the below code example)

using System.Collections.Generic;

namespace MainNS
{
    public interface IStructInterface
    {
        string Name { get; }
    }

    public struct StructImplementation : IStructInterface
    {
        public string Name
        {
            get { return "Test"; }
        }
    }

    public class MainClass
    {
        public static void Main()
        {
            StructImplementation[] structCollection = new StructImplementation[1]
            {
                new StructImplementation()
            };

            // Perform an implicit cast
            IEnumerable<IStructInterface> castCollection = structCollection;    // Invalid implicit cast
        }
    }
}

When compiling the above code, I get the error:

error CS0029: Cannot implicitly convert type ‘MainNS.StructImplementation[]’ to ‘MainNS.IStructInterface[]’

If I change StructImplementation to a class I have no problems, so I’m assuming what I’m trying to do is either not valid; or I’m being blind and missing something obvious.

Any advice or explanation for this would be appreciated.

EDIT

In case anyone else has this issue and using a different approach is less than ideal (as was the case in my situation), I worked around my issue using the LINQ method Cast<T>(). So in the example above, I would perform the cast using something like:

IEnumerable<IStructInterface> castCollection = structCollection.Cast<IStructInterface>();

There is a good article on MSDN about Variance in Generic Types, which I found very useful.

  • 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-27T07:20:26+00:00Added an answer on May 27, 2026 at 7:20 am

    Array variance ony allows for the reference preserving case, and so only works for classes. It is essentially treating the original data as a reference to a different type. This is simply not possible with structs.

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

Sidebar

Related Questions

I have a struct that looks something like this: [StructLayout(LayoutKind.Sequential)] public struct in_addr {
I have a struct that I initialize like this: typedef struct { word w;
I have this routine that calculates the seconds-to-date for a struct tm . On
I have some C++ code that implements a basic Pixel class. Here's just a
Let's say I have a struct that contains local environments: public struct Environments {
I have a 3rd party struct that is comprised of the following: [StructLayout(LayoutKind.Sequential, Size=1)]
In my C# application, I have a large struct (176 bytes) that is passed
I have an unmanaged struct I'd like to marshal to c sharp that looks
I have a function that takes a struct, and I'm trying to store its
I have a class that has an inner struct/class (I have decided which yet,

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.