1

I apologies if this is a duplicate question;

I can find out the number of elements in array:

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour
{

    string[] myThings = new string[] {"Banana", "Dinosaur", "Gwen Stefani"};
    // int l = myThings.Length; // FAIL!

    // Use this for initialization
    void Start ()
    {
        ProcessThings();
    }

    void ProcessThings ()
    {
        //int l = myThings.Length;
        print("Things: " + l);
    }

}

My question is this why can't I declare l= myThings.Length outside of method (line 9)? A field initializer cannot reference the nonstatic field, method, or property `Test.myThings'

Go easy on me, I'm learning :)

Moving up from crayons to C#

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
Ghoul Fool
  • 6,249
  • 10
  • 67
  • 125
  • You don't need to assign l, Length is a property is myThings, so you can print("Things: " + myThings.Length); – Cookie Mar 05 '14 at 11:41
  • Because "A field initializer cannot reference the nonstatic field, method, or property 'Test.myThings'". It's how C# works. Use `myThings.Length`, it's not so long, or if you use it a lot in some method, declare local var as in your comment – Andriy Tylychko Mar 05 '14 at 11:41
  • Please, do not include information about a language used in a question title unless it wouldn't make sense without it. Tags serve this purpose. – Ondrej Janacek Mar 05 '14 at 11:52

4 Answers4

2

This is generally not allowed because, the compiler might rearrange the variables and there is no guarantee that the field myThings would be initialized before its length is assigned to l.

As an alternative you can initialize the field l in the constructor.

Suresh Kumar Veluswamy
  • 4,193
  • 2
  • 20
  • 35
  • _"the compiler might rearrange the variables"_ -- no, the compiler won't do that. The language spec prohibits it. See [correct answer](http://stackoverflow.com/a/22196873) posted by Ondrej. – Peter Duniho Mar 08 '17 at 22:50
1

Explanation from C# specs.

10.5.5.2 Instance Field Initialization

A variable initializer for an instance field cannot reference the instance being created. Thus it is a compile-time error to reference this in a variable initializer, because it is a compiletime error for a variable initializer to reference any instance member through a simple-name. In the example

class A
{
    int x = 1;
    int y = x + 1; // Error: reference to instance member of this
}

the variable initializer for y results in a compile-time error because it references a member of the instance being created.

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
0

"You cannot use an instance variable to initialize another instance variable. Why? Because the compiler can rearrange these - there is no guarantee that reminder will be initialized before defaultReminder, so the above line might throw a NullReferenceException."

see this link: A field initializer cannot reference the nonstatic field, method, or property

(btw, to do this add static before your type)

Community
  • 1
  • 1
AsfK
  • 3,328
  • 4
  • 36
  • 73
0

You are assuming the string array "mythings" is initialized before the integer member variable, by trying to use a property from the string array to initialize the integer.

The order of initialization is not guaranteed. So you cannot do anything which is relying on the order of initialization.

Jimmy
  • 3,224
  • 5
  • 29
  • 47
  • _"The order of initialization is not guaranteed"_ -- not true. The order _is_ guaranteed. The language disallows this for other reasons (primarily, for comprehensibility of the code). See [correct answer](http://stackoverflow.com/a/22196873) posted by Ondrej. – Peter Duniho Mar 08 '17 at 22:52