Object reference not found in Main()

Hello

When i try to build this

class Class1
      {
            static void Main(string[] args)
            {
                  Console.WriteLine("default:\t{0}",s);
                  Console.WriteLine("lowercase:\t{0}",s.ToLowerCase);
            }
            string s = "C# Programming for me";
      }

I get this error "An object reference is required for the nonstatic field, method, or property 'PigLatin.Class1.s' "

Is this because Main is a static method and cannot operate with nonstatic fields? I thought that s that i use in the WriteLine statement is the actual reference to the string object i declared at class level. So why cant it find it?
Kokas79Asked:
Who is Participating?
 
NipNFriar_TuckCommented:
You are correct, main is a static field and cannot access an instance level variable.  Try doing this...

class Class1
     {
          static void Main(string[] args)
          {
                Class1 c = new Class1();

               Console.WriteLine("default:\t{0}",c.s);
               Console.WriteLine("lowercase:\t{0}",c.s.ToLowerCase);
          }
          public string s = "C# Programming for me";
     }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.