Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><em>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".</em></p> <p>The other answers have already given you a quick way to <em>fix</em> your problem (just make <code>Add</code> a <code>static</code> function), but I'd like to explain why.</p> <p>C# has a fundamentally different design paradigm than C. That paradigm is called <strong>object-oriented programming</strong> (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.</p> <p>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 <code>Program</code> knows how to perform <code>Add</code>. Said another way, you have to create an instance of <code>Program</code>, and then ask <code>Program</code> to perform an <code>Add</code> for you.</p> <p>The solutions that people gave you, using the <code>static</code> keyword, route around that design. Using the <code>static</code> 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 <code>Add</code> 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.</p> <p>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. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload