[c#] C# Creating and using Functions

I need help with C# programming; I am new to it and I come from C background. I have a Console Application like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Add_Function
{
   class Program
   {
      static void Main(string[] args)
      {
         int a;
         int b;
         int c;

         Console.WriteLine("Enter value of 'a':");
         a = Convert.ToInt32(Console.ReadLine());

         Console.WriteLine("Enter value of 'b':");
         b = Convert.ToInt32(Console.ReadLine());

         //why can't I not use it this way?
         c = Add(a, b);
         Console.WriteLine("a + b = {0}", c);
      }//END   Main

      public int Add(int x, int y)
      { 
         int result = x + y;
         return result;
      }//END   Add
   }//END      Program
}//END         Add_Function

It gives me this error on the line that I call Add():

An object reference is required for the non-static field, method, or property 'Add_Function.Program.Add(int, int)'

Can anyone please explain to me why I have this problem. Is this because the architecture of C# is different than C, and the way I call it is wrong? Thanks.

This question is related to c# function

The answer is


Note: in C# the term "function" is often replaced by the term "method". For the sake of this question there is no difference, so I'll just use the term "function".

The other answers have already given you a quick way to fix your problem (just make Add a static function), but I'd like to explain why.

C# has a fundamentally different design paradigm than C. That paradigm is called object-oriented programming (OOP). Explaining all the differences between OOP and functional programming is beyond the scope of this question, but here's the short version as it applies to you.

Writing your program in C, you would have created a function that adds two numbers, and that function would exist independently and be callable from anywhere. In C# most functions don't exist independently; instead, they exist in the context of an object. In your example code, only an instance (an object) of the class Program knows how to perform Add. Said another way, you have to create an instance of Program, and then ask Program to perform an Add for you.

The solutions that people gave you, using the static keyword, route around that design. Using the static keyword is kind of like saying, "Hey, this function I'm defining doesn't need any context/state, it can just be called." Since your Add function is very simple, this makes sense. As you start diving deeper into OOP, you're going to find that your functions get more complicated and rely on knowing their state/context.

My advice: Pick up an OOP book and get ready to switch your brain from functional programming to OOP programming. You're in for a ride.


The reason why you have the error is because your add function is defined after your using it in main if you were to create a function prototype before main up above it with public int Add(int x, int y); or you could just copy and paste your entire Add function above main cause main is where the compiler starts execution so doesn't it make sense to declare and define a function before you use it hope that helps. :D


you have to make you're function static like this

namespace Add_Function
{
  class Program
  {
    public static void(string[] args)
    {
       int a;
       int b;
       int c;

       Console.WriteLine("Enter value of 'a':");
       a = Convert.ToInt32(Console.ReadLine());

       Console.WriteLine("Enter value of 'b':");
       b = Convert.ToInt32(Console.ReadLine());

       //why can't I not use it this way?
       c = Add(a, b);
       Console.WriteLine("a + b = {0}", c);
    }
    public static int Add(int x, int y)
    {
        int result = x + y;
        return result;
     }
  }
}

you can do Program.Add instead of Add you also can make a new instance of Program by going like this

Program programinstance = new Program();

What that build error is telling you, that you have to either have an instance of Program or make Add static.


static void Main(string[] args)
    {
        Console.WriteLine("geef een leeftijd");
        int a = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("geef een leeftijd");
        int b = Convert.ToInt32(Console.ReadLine());

        int einde = Sum(a, b);
        Console.WriteLine(einde);
    }
    static int Sum(int x, int y)
    {
        int result = x + y;
        return result;

You should either make your Add function static like so:

static public int Add(int x, int y)
{ 
    int result = x + y;
    return result;
 } //END   Add

static means that the function is not class instance dependent. So you can call it without needing to create a class instance of Program class.

or you should create in instance of your Program class, and then call Add on this instance. Like so:

Program prog = new Program();
prog.Add(5,10);

This code gives you an error because your Add function needs to be static:

static public int Add(int x, int y)

In C# there is a distinction between functions that operate on instances (non-static) and functions that do not operate on instances (static). Instance functions can call other instance functions and static functions because they have an implicit reference to the instance. In contrast, static functions can call only static functions, or else they must explicitly provide an instance on which to call a non-static function.

Since public static void Main(string[] args) is static, all functions that it calls need to be static as well.


static void Main(string[] args)
    {
        Console.WriteLine("geef een leeftijd");
        int a = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("geef een leeftijd");
        int b = Convert.ToInt32(Console.ReadLine());

        int einde = Sum(a, b);
        Console.WriteLine(einde);
    }
    static int Sum(int x, int y)
    {
        int result = x + y;
        return result;
    }

Note: in C# the term "function" is often replaced by the term "method". For the sake of this question there is no difference, so I'll just use the term "function".

Thats not true. you may read about (func type+ Lambda expressions),( anonymous function"using delegates type"),(action type +Lambda expressions ),(Predicate type+Lambda expressions). etc...etc... this will work.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

           int a;
           int b;
           int c;

           Console.WriteLine("Enter value of 'a':");
           a = Convert.ToInt32(Console.ReadLine());

           Console.WriteLine("Enter value of 'b':");
           b = Convert.ToInt32(Console.ReadLine());

           Func<int, int, int> funcAdd = (x, y) => x + y;
           c=funcAdd.Invoke(a, b);
           Console.WriteLine(Convert.ToString(c));

        }

     }
}

Just make your Add function static by adding the static keyword like this:

public static int Add(int x, int y)

Because your function is an instance or non-static function you should create an object first.

Program p=new Program();
p.Add(1,1)