Lesson 10 of 15
Methods
Methods
Methods are reusable blocks of code. In C#, they can have parameters and return values:
static int Add(int a, int b) {
return a + b;
}
Console.WriteLine(Add(3, 4)); // 7
Expression Body
Short methods can use => (arrow syntax):
static int Square(int n) => n * n;
static string Greet(string name) => $"Hello, {name}!";
Multiple Return Values with Tuples
static (int min, int max) MinMax(int[] arr) {
return (arr.Min(), arr.Max());
}
var (lo, hi) = MinMax(new[] { 3, 1, 4, 1, 5 });
Console.WriteLine($"{lo} {hi}"); // 1 5
Your Task
Write a static method IsPrime(int n) that returns true if n is a prime number, false otherwise.
WasmSharp (.NET) loading...
Loading...
Click "Run" to execute your code.