--- title: "C# Guides: Modularity, Encapsulation, Lambdas, Interoperability, and Asynchronous Programming" --- # A Guide to Modularity, Encapsulation, Lambdas, Interoperability, and Asynchronous Programming [TOC] --- ## Modularity Modularity is a software design principle that promotes breaking a large, complex system into smaller, self-contained modules that can be developed, tested, and maintained independently. Here is an example in C# code: ```csharp= using System; namespace Module1 { public class Module1Class { public void Method1() { Console.WriteLine("Method from Module1"); } } } namespace Module2 { public class Module2Class { public void Method2() { Console.WriteLine("Method from Module2"); } } } class Program { static void Main() { var module1 = new Module1.Module1Class(); var module2 = new Module2.Module2Class(); module1.Method1(); module2.Method2(); } } ``` In this example, we have two namespaces (Module1 and Module2) representing different modules. Each module contains its own classes and methods, making the code modular and maintainable. --- ## Encapsulation Encapsulation is one of the fundamental principles of object-oriented programming (OOP), which involves bundling the data (attributes) and the methods (functions) that operate on the data into a single unit called a class. Access to the data is controlled through public, private, and protected access modifiers. Here is an example in C# code: ```csharp= class Employee { private string name; private double salary; public Employee(string name, double salary) { this.name = name; this.salary = salary; } public void SetSalary(double newSalary) { if (newSalary > 0) { salary = newSalary; } } public double GetSalary() { return salary; } public string GetName() { return name; } } class Program { static void Main() { Employee emp = new Employee("John", 50000.0); emp.SetSalary(55000.0); Console.WriteLine($"{emp.GetName()}'s salary is ${emp.GetSalary()}"); } } ``` In this example, the Employee class encapsulates the name and salary fields and provides public methods for setting and getting these values, allowing controlled access to the data. --- ## Lambdas Lambdas are anonymous functions that can be used to create delegates or expression trees. They are useful for writing concise, inline functions. Here is an example in C# code: ```csharp= using System; using System.Collections.Generic; class Program { static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; // Using a lambda expression to find even numbers List<int> evenNumbers = numbers.FindAll(x => x % 2 == 0); Console.WriteLine("Even numbers:"); evenNumbers.ForEach(num => Console.WriteLine(num)); } } ``` In this example, we use a lambda expression in the FindAll method to filter even numbers from a list. --- ## Interoperability Interoperability refers to the ability of a programming language to work with other languages or systems. In C#, you can achieve interoperability by using platform invoke (P/Invoke) to call native functions in DLLs, or by using libraries like .NET Core's Platform Invocation Services (P/Invoke) or COM interop. Here is an example in C# code: ```csharp= using System; using System.Runtime.InteropServices; class Program { [DllImport("user32.dll")] static extern int MessageBox(IntPtr hWnd, string text, string caption, uint type); static void Main() { MessageBox(IntPtr.Zero, "Hello, Interoperability!", "Message", 0x00000040); } } ``` In this example, we use P/Invoke to call the MessageBox function from the user32.dll library, which is a Windows native function. --- ## Asynchronous Programming Asynchronous programming allows you to write code that doesn't block the main thread, making your applications more responsive. In C#, you can use the async and await keywords to write asynchronous code. Here is an example in C# code: ```csharp= using System; using System.Net.Http; using System.Threading.Tasks; class Program { static async Task Main() { await DownloadDataAsync(); } static async Task DownloadDataAsync() { using (var client = new HttpClient()) { string url = "https://jsonplaceholder.typicode.com/posts/1"; HttpResponseMessage response = await client.GetAsync(url); if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); Console.WriteLine(content); } } } } ``` In this example, we use asynchronous methods to download data from a web server without blocking the main thread, improving application responsiveness.