[C#] C#: "The assignment to a constant array must be a constant"

recommended reading

Hello everyone, I am a Buddhist engineer ☆Tranquil Little Demon Dragon☆ . I update Unity development skills from time to time. If you think it is useful, remember to click three times.

I. Introduction

Record an interesting code snippet.

First, a review of constants.

Because this article is not intended to specifically explain constants, let’s briefly introduce constants:

Constant composition:

A constant in C# is composed of three parts, the decibel is const, data type, and variable name.

  • 1. const: It is a keyword used to define constants in C# and cannot be ignored.
  • 2. Data type: Specify a type of data, such as integer, string, etc.
  • 3. Variable name: custom naming.

for example:

const int sum = 0;

What is a constant
Constant means that its value will not change during the running of the program. The only difference between a constant and a variable is that the value of a constant cannot be changed, while the value of a variable can be changed.

Why use a constant
Since the value of a constant cannot be changed during the running of the program, it can be used to store some fixed values, such as pi, discount rate, etc.

for example:

const float PI = 3.14159265358979F;

So, when the blogger wrote the code, he thought, can we define a constant array, and store constants in it. . .

Two, the text

So with this code:

using UnityEngine;

public class Test01 : MonoBehaviour
{
    
    
    const int[] sumAry = new int[1] {
    
     1 };

    void Start()
    {
    
    
        
    }
}

But writing this way, an error is reported, which is a very serious error that cannot be compiled:
insert image description here
this is because constthe keyword is used to compile known values, but the array will not be initialized at compile time, so the value of the array is not known at compile time, so It's pointless to write this way.

However, keywords can be used readonlyto achieve the same effect without error.

readonlyreadonlyKeywords are used to specify that the value of a variable cannot be modified after initialization. The following code example shows us how to use keywords in C# :

using System.Runtime.CompilerServices;
using UnityEngine;

public class Test01 : MonoBehaviour
{
    
    
    public static readonly string[] Values = {
    
     "Value1", "Value2", "Value3", "Value4" };

    void Start()
    {
    
    
        Debug.Log(Values[0]);
    }
}

3. Postscript

If you find this article useful, don’t forget to follow it, follow it so you don’t get lost, and continue to share more Unity dry goods articles.


Your likes are your support for bloggers, please leave a message if you have any questions:

The blogger's homepage has contact information.

The blogger also has many treasured articles waiting for your discovery:

column direction Introduction
Unity3D development small game Small Game Development Tutorial Share some small games developed using the Unity3D engine, and share some tutorials for making small games.
Unity3D from entry to advanced getting Started Get inspiration from self-study Unity, summarize the route of learning Unity from scratch, and have knowledge of C# and Unity.
UGUI for Unity3D UGUI A full analysis of Unity's UI system UGUI, starting from the basic controls of UGUI, and then comprehensively teaching the principles of UGUI and the use of UGUI.
Reading data of Unity3D file read Use Unity3D to read txt files, json files, xml files, csv files, and Excel files.
Data collection of Unity3D data set Array collection: Knowledge sharing of data collections such as arrays, lists, dictionaries, stacks, and linked lists.
VR/AR (Virtual Simulation) Development of Unity3D virtual reality Summarize the common virtual simulation needs of bloggers and give case explanations.
Plugin for Unity3D plug-in Mainly share some plug-in usage methods used in Unity development, plug-in introduction, etc.
Daily development of Unity3D daily record It is mainly used by bloggers in daily development, methods and skills used, development ideas, code sharing, etc.
Daily BUG of Unity3D daily record Record the bugs and pitfalls encountered during the development of the project using the Unity3D editor, so that later generations can have some reference.

Guess you like

Origin blog.csdn.net/q764424567/article/details/132560481