简体   繁体   中英

C# array member

Here is my code and it gives red squiggly line under the variable of the array declaration, stating,

Fixed size buffer fields may only be members of struct

float is a value type, so I don't quite understand this message. What is the correct syntax to create public array of 13 elements (0 through 12; I ignore index 0)...

class clsUtility
{
    public int UtilityId { get; set; }
    public fixed float InterpolationFactorMonth[13];      // <-- HERE IS THE PROBLEM
}

If you want a fixed-sized array inline, that has to be declared within a struct, as per the compiler error. If you're actually happy with a reference to an array as normal, you need to differentiate between declaration and initialization. For example:

// Note: Utility isn't a great name either, but definitely lose the "cls" prefix.
class Utility
{
    public int UtilityId { get; set; }
    public float[] InterpolationFactorMonth { get; } = new float[13];
}

This declares a read-only property of type float[] , and initializes it with a new array of 13 elements.

So first of all, it is not possible to use fixed in this case. You could use public float[] InterpolationFactorMonth = new float[13]; To create an array of the size 13.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM