-4

Why I can not use this instance?

    class MainClass
    {

        List<int> d = new List<int> (5);
        // d[0] error
        public static void Main (string[] args)
        {
            // d[0] error 
        }
    }

Why I can not use this object?

Person.Junkie
  • 1,816
  • 4
  • 21
  • 31
  • 1
    Can you clarify this a bit? What can't you use? Where? – rory.ap Feb 12 '15 at 18:00
  • 1
    When you get an error, *always* include it in the question... but only ask the question after you've searched for other questions about the same error, of which there are *lots*. – Jon Skeet Feb 12 '15 at 18:02
  • 1
    `d[0]` is not the instance of the list. It is one of the elements in the list. – Brian Rasmussen Feb 12 '15 at 18:02
  • @BrianRasmussen And getting that one item in the list requires first accessing the instance of the list, which he can't do. This question [has a number of problems](http://stackoverflow.com/questions/28484537/why-i-can-not-access-instance-of-list#comment45291069_28484537), but that isn't really one of them. – Servy Feb 12 '15 at 18:03
  • I get that - I was just trying to clarify. – Brian Rasmussen Feb 12 '15 at 18:04
  • Static List d = new List (5); – ako Feb 12 '15 at 18:10

1 Answers1

6

Your method is static and your field is not. That's why you can't access it.

If you read the error message you'd be able to figure it out by yourself:

An object reference is required for the non-static field, method, or property 'SOTestProject.MainClass.d'

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263