C# Struct No Parameterless Constructor? See what I need to accomplish

I am using a struct to pass to an unmanaged DLL as so –

[StructLayout(LayoutKind.Sequential)]
        public struct valTable
        {
            public byte type;
            public byte map;
            public byte spare1;
            public byte spare2;
            public int par;
            public int min;
            public byte[] name;
            public valTable()
            {
                name = new byte[24];
            }
        }

The code above will not compile because VS 2005 will complain that “Structs cannot contain explicit parameterless constructors”. In order to pass this struct to my DLL, I have to pass an array of struct’s like so valTable[] val = new valTable[281];

What I would like to do is when I say new, the constructor is called and it creates an array of bytes like I am trying to demonstrate because the DLL is looking for that byte array of size 24 in each dimension.

How can I accomplish this?

hide parameterless constructor on struct

Is it possible to hide the parameterless constructor from a user in c# I want to force them to always use the constructor with parameters e.g. this Position class public struct Position { private read

Array struct requires parameterless constructor

I have written an Array struct (yes I know they already exist elsewhere, but I want to create my own). Why does my code require the item that I add has a parameterless constructor? template <typena

Why do I need a parameterless constructor? [duplicate]

Possible Duplicate: Why XML-Serializable class need a parameterless constructor I’m getting the run-time error as stated below. Message=OutlookAddIn1.DeviceRegistrationRequest cannot be serialized

Why struct can not have parameterless constructor [duplicate]

Why struct can not have parameterless constructor? What’s the problem in doing this for CLR or why it’s not allowed ? Please explain it as I don’t understand it.

Struct Implicit Default Constructor vs. Parameterless Constructor

OK, we hear that structcannot have a default parameterless constructor which is fine (http://stackoverflow.com/questions/333829/why-cant-i-define-a-default-constructor-for-a-struct-in-net). But docume

C# alternate parameterless constructor

Is it possible to define an alternate parameterless constructor for a C# class? In other words, I have a class Foo. I want to have a default constructer Foo() and another constructor SpecialFoo(). I d

What is the purpose of private parameterless constructor in C#

I just received C# in Depth by Jon Skeet in the mail and am not following the discussion on page 7-8. We now have a private parameterless constructor for the sake of the new property-based initializ

What Are the Benefits of the Object class having a public parameterless constructor?

I have been trying for some time to determine why the Object class has a public, parameterless constructor or, indeed, why it is not marked abstract. I cannot see a reasonable circumstance where it wo

I have a parameterless public constructor but I am still getting an error saying I need one

The error is: An error occurred while creating a controller of type ‘foo’. If the controller doesn’t have a controller factory, ensure that it has a parameterless public constructor. However, fooCon

MVC Controller with no parameterless constructor

Right now I am learning MVC, but have running into a problem that I can’t figure out. In a test project I have downloaded I can see that some controllers doesn’t have a parameterless constructor, inst

Answers

For what you need to do you really need a class I think. A struct already implements a parameterless constructor by default which is why you cant define another one.

Struct constructors are similar to class constructors, except for the following differences:

  • Structs cannot contain explicit parameterless constructors. Struct members are automatically initialized to their default values.
  • A struct cannot have an initializer in the form: base (argument-list).

This means that

A default(parameterless) constructor for a struct could set different
values than the all-zeroed state which would be unexpected behavior. The
.Net Runtime therefore prohabits default constructors for struct.

The typical way to get around this scenario is to create a static method that will create your new instance, initialize it the way you want, and return it. This is the way it is done in .NET to get structures initialized with specific values.


ref; Structs cannot contain explicit parameterless constructors. WHY?

Not the cleanest fix, but you could just add a parameter and never use it?

[StructLayout(LayoutKind.Sequential)]
    public struct valTable
    {
        public byte type;
        public byte map;
        public byte spare1;
        public byte spare2;
        public int par;
        public int min;
        public byte[] name;
        public valTable(int x)
        {
            name = new byte[24];
        }
    }

You can use a fixed size buffer – which I suspect you really want anyway, so as to get the data “inline” in the struct (rather than a reference to an array elsewhere).

public fixed byte name[24];

You’ll need to declare the struct as unsafe as well though.

Note that any “solution” which requires calling a static method or providing any kind of custom constructor will fail with your explicit goal of being able to create an array of these structs.

I would recommend to write this code.

[StructLayout(LayoutKind.Sequential)]
public struct valTable
{
    public byte type;
    public byte map;
    public byte spare1;
    public byte spare2;
    public int par;
    public int min;
    public byte[] name;

    static public valTable Create()
    {
        valTable vT = new valTable();
        vT.name = new byte[24];
        return vT;
    }
}

Building on Asad Butt’s answer, you can create a static method to perform the work of your constructor like so:

[StructLayout(LayoutKind.Sequential)]
public struct valTable
{
    public byte type;
    public byte map;
    public byte spare1;
    public byte spare2;
    public int par;
    public int min;
    public byte[] name;
    public valTable()

    public static valTable NewTable()
    {
        valTable tbl = new valTable();
        tbl.name = new byte[24];
        return tbl;
    }
}

You’ll see classes in the .NET Framework following this pattern already. Guid.NewGuid() immediately springs to mind. There may be others.