Notifications
Clear all
Topic starter 01/08/2025 11:48 pm
In .NET (especially using C#), functions are typically defined as methods—named blocks of code that perform tasks, can take input, and optionally return a result. They’re essential for organizing and reusing logic.
🏗️ Basic Function Syntax
public int Add(int a, int b)
{
return a + b;
}
public
: access modifier (others includeprivate
,protected
, etc.)int
: return typeAdd
: function name(int a, int b)
: parametersreturn
: sends result back
Call it like:
int result = Add(3, 4); // result is 7
🧠 Void Functions (No Return)
public void Greet(string name)
{
Console.WriteLine($"Hello, {name}!");
}
✨ Overloaded Functions
Same name, different parameters:
public void Print(string text) { Console.WriteLine(text); }
public void Print(int number) { Console.WriteLine(number); }
🎯 Static vs. Instance Functions
- Static: belong to the class
public static void SayHi() { Console.WriteLine("Hi"); }
- Instance: belong to objects
myDog.Speak(); // assuming Speak() is a method on a Dog instance
🧩 Optional and Named Parameters
public void SendEmail(string to, string subject = "No Subject") { ... }
SendEmail("someone@example.com"); // uses default subject
🌐 Async Functions
For non-blocking tasks like web calls:
public async Task<string> FetchDataAsync()
{
// async logic here
}