Functions in C#
Functions are central in C#. We’ll learn about functions with a few different examples.
To run the program, save it as Functions.cs
and use the C# compiler to compile and run:
There are several other features to C# functions. One is multiple return values, which we can achieve using tuples or out parameters.
In C#, functions are defined using the static
keyword when they belong to the class itself rather than an instance of the class. The Main
method is the entry point of the program.
C# uses Console.WriteLine
for printing to the console, which is similar to fmt.Println
in the original example.
Unlike Go, C# uses curly braces {}
to define function bodies and doesn’t use semicolons to end statements (although they are optional).
C# also supports method overloading, allowing multiple methods with the same name but different parameters, which wasn’t shown in this example but is a powerful feature of the language.