Forum

Learn .Net Function…
 
Notifications
Clear all

Learn .Net Functions

1 Posts
1 Users
0 Reactions
10 Views
 josh
(@josh)
Member Admin
Joined: 2 months ago
Posts: 510
Topic starter  

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 include private, protected, etc.)
  • int: return type
  • Add: function name
  • (int a, int b): parameters
  • return: 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
}

 


   
Quote
Share: