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
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.
Just make your Add
function static
by adding the static
keyword like this:
public static int Add(int x, int y)
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.
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;
What that build error is telling you, that you have to either have an instance of Program
or make Add
static.
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