简体   繁体   中英

An object reference is required for the non-static field, method, or property DoStuff(int, int)'

using System;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int number1,number2;
            number1 = int.Parse(Console.ReadLine());
            number2 = int.Parse(Console.ReadLine());
            Console.WriteLine("the sum of numbers are " +" "+ (number1 + number2));
            Console.ReadKey();
            DoStuff(number1,number2);
        }

        public int DoStuff(int num1,int num2)
        {
            int result = num1 + num2;
            return result;
        }

    }
}

What am I doing wrong?

You are not allowed to call a non-static method inside a static method. If you want to invoke then you need a object reference. This is because the static methods cannot be instantiate. So the method DoStuff should be static, hence its signature may looks like this:

public static int DoStuff(int num1,int num2)
{
    int result = num1 + num2;
    return result;
} 

I have one more suggestion for you to improve your code; it is nothing but use int.TryParse instead for simple .Parse . Which helps you to handle FormatException too. So the complete code will looks like:

class Program
{
    static void Main(string[] args)
    {
        int number1, number2;
        if (int.TryParse(Console.ReadLine(), out number1) && int.TryParse(Console.ReadLine(), out number2))
        {
            Console.WriteLine("the sum of numbers are :{0}", DoStuff(number1, number2));
        }
        else
            Console.ReadKey();
    }
    public static int DoStuff(int num1, int num2)
    {
        int result = num1 + num2;
        return result;
    }

}

From a static function you can call only static functions. So change your DoStuff function to be static:

public static int DoStuff(int num1,int num2)
{
    int result = num1 + num2;
    return result;
}

For more info: Static classes

A non-static member belongs to an instance. It's meaningless without somehow resolving which instance of a class you are talking about. In a static context, you don't have an instance, that's why you can't access a non-static member without explicitly mentioning an object reference.

In fact, you can access a non-static member in a static context by specifying the object reference explicitly:

class HelloWorld {
   int i;
   public HelloWorld(int i) { this.i = i; }
   public static void Print(HelloWorld instance) {
      Console.WriteLine(instance.i);
   }
}

var test = new HelloWorld(1);
var test2 = new HelloWorld(2);
HelloWorld.Print(test);

Without explicitly referring to the instance in the Print method, how would it know it should print 1 and not 2?

You are using DoStuff as a static method.

You should do the following:

...
Console.ReadKey();
var program = new Program();
program.DoStuff(number1, number2);
...

So create an instance of your class and call the method upon it.

If you do want to use the method in a static way, you should declare it statically:

public static int DoStuff(int num1,int num2)
{
    int result = num1 + num2;
    return result;
} 

See the "static" keyword after "public".

From C# Specifications:

1.6.6.3 Static and instance methods

A method declared with a static modifier is a static method. A static method does not operate on a specific instance and can only directly access static members. A method declared without a static modifier is an instance method. An instance method operates on a specific instance and can access both static and instance members. The instance on which an instance method was invoked can be explicitly accessed as this. It is an error to refer to this in a static method.

Your Main method is static, so it can only access static members. Solution is to change:

public static int DoStuff(int num1,int num2)

Either you can stuff the Method with static as show in below or

public static  int DoStuff(int num1,int num2)
 {
    int result = num1 + num2;
    return result;
 }

Static methods :These are called with the type name. No instance is required—this makes them slightly faster. Static methods can be public or private.

Info: Static methods use the static keyword, usually as the first keyword or the second keyword after public. Warning: A static method cannot access non-static class level members. It has no "this" pointer.

Instance: An instance method can access those members, but must be called through an instantiated object. This adds indirection.

For Example :

class Program
   {
    static void MethodA()
    {
        Console.WriteLine("Static method");
    }

    void MethodB()
    {
        Console.WriteLine("Instance method");
    }

    static char MethodC()
    {
        Console.WriteLine("Static method");
        return 'C';
    }

    char MethodD()
    {
        Console.WriteLine("Instance method");
        return 'D';
    }

    static void Main()
    {
        //
        // Call the two static methods on the Program type.
        //
        Program.MethodA();
        Console.WriteLine(Program.MethodC());
        //
        // Create a new Program instance and call the two instance methods.
        //
        Program programInstance = new Program();
        programInstance.MethodB();
        Console.WriteLine(programInstance.MethodD());
        Console.ReadLine();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM