C# Creating and using Functions

后端 未结 11 638
暗喜
暗喜 2020-12-28 18:08

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         


        
相关标签:
11条回答
  • 2020-12-28 18: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.

    0 讨论(0)
  • 2020-12-28 18:13

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

    public static int Add(int x, int y)
    
    0 讨论(0)
  • 2020-12-28 18:18

    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.

    0 讨论(0)
  • 2020-12-28 18:21
    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;
    
    0 讨论(0)
  • 2020-12-28 18:23

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

    0 讨论(0)
  • 2020-12-28 18:23

    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

    0 讨论(0)
提交回复
热议问题