0

I am new to C#, sorry if this is duplicate. I am simply trying to add two variables together but I get the error from the title

An object reference is required for the non-static field, method, or property 'Program.x'

Here is code

using System;

namespace HelloWorld
{
  class Program
  {
    int x = 3;
    int y = 10;
    static void Main(string[] args)
    {
        int mathResults = x + y;

        string results = mathResults.ToString();

        Console.WriteLine("Hello World!");    
    }
  }
}

Could someone please explain WHY I am getting this error? Thank you!

jonthey12
  • 3
  • 1

1 Answers1

0

As your Main() method is static, you must use static variables in it.

So it should be like this :

class Program
  {
    static int x = 3;
    static int y = 10;

    static void Main(string[] args)
    {
        int mathResults = x + y;

        string results = mathResults.ToString();

        Console.WriteLine("Hello World!");    
    }
  }
Rivo R.
  • 351
  • 2
  • 8