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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:51:09+00:00 2026-05-18T08:51:09+00:00

I have a struct (.NET 3.5): struct ColumnHeadings { public string Name ; public

  • 0

I have a struct (.NET 3.5):

struct ColumnHeadings 
        { 
            public string Name ; 
            public int Width ; 
        } ;

And when I try to assign a list of values to that struct I get a ‘cannot implicitly convert type string/int to …’:

private void doSomething()
{
    ColumnHeadings[,] ch = new ColumnHeadings[,]{{"column1",100},
                {"column2",100},{"column3",100}};
}

Can the struct values be assigned in the same way as a multi-dimensional array? Or do I need to assign the values by using?:

ch.Name = "column 1";

UPDATE:

Thanks to Marc’s excellent feedback the correct solution is:

Struct:

struct ColumnHeadings
        {
            private readonly string name;
            private readonly int width;
            public string Name { get { return name; } }
            public int Width { get { return width; } }
            public ColumnHeadings(string name, int width)
            {
                this.name = name;
                this.width = width;
            }
        } 

Then in the method:

 var ch = new[]{new ColumnHeadings("column1",100),
            new ColumnHeadings("column2",100),
            new ColumnHeadings("column3",100)};

And the link to why mutuable structs aren’t a good idea.

  • 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-18T08:51:09+00:00Added an answer on May 18, 2026 at 8:51 am

    firstly, that probably shouldn’t be a struct at all

    The syntax will be:

    ColumnHeadings[] ch = new ColumnHeadings[]{
        new ColumnHeadings{Name="column1",Width=100},
        new ColumnHeadings{Name="column2",Width=100}
    };
    

    However, in addition you have the issue of public fields, and the fact that this is a mutable struct – both of which are dangerous. No, really.

    I would add a constructor:

    var ch = new []{
         new ColumnHeadings("column1", 100),
         new ColumnHeadings("column2", 100)
    };
    

    with:

    struct ColumnHeadings
    {
        private readonly string name;
        private readonly int width;
        public string Name { get { return name; } }
        public int Width { get { return width; } }
        public ColumnHeadings(string name, int width)
        {
            this.name = name;
            this.width = width;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In ASP.NET C# I have a struct: public struct Data { public int item1;
I have this struct: struct Map { public int Size; public Map ( int
I have the following .NET value types: [StructLayout(LayoutKind.Sequential)] public struct Date { public UInt16
.NET we have primitive datatypes like int and value types like struct. And also
In .NET, a value type (C# struct ) can't have a constructor with no
Say I have a struct s with an int pointer member variable i. I
In my C# application, I have a large struct (176 bytes) that is passed
I have a method that deals with some geographic coordinates in .NET, and I
I have constructed several web services that successfully serialize my .NET types into JSON.
I have a struct defined like follows as part of an object. I'm trying

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.