Ehsan-org / ASP.NET-FirstProject

1 stars 0 forks source link

Research on the following concepts: Async/Sync/Void/Await/Save/SaveAsync #45

Open ehsan-nadernezhad opened 1 month ago

ehsan-nadernezhad commented 1 month ago

Async (Asynchronous) Definition: Asynchronous programming allows a program to run tasks in the background without blocking the main thread. This is useful for tasks that involve I/O operations, such as reading from a file or making a network request.

Example: Using async keyword in C#:

csharp

Copy public async Task GetDataAsync() { // Asynchronous code here } Sync (Synchronous) Definition: Synchronous programming means that tasks are executed one after another, blocking the main thread until each task completes.

Example: Using synchronous code in C#:

csharp

Copy public int GetData() { // Synchronous code here } Void Definition: The void keyword indicates that a method does not return a value.

Example: A method that performs an action but doesn't return anything:

csharp

Copy public void PrintMessage() { Console.WriteLine("Hello, World!"); } Await Definition: The await keyword is used in an asynchronous method to pause execution until a task completes, without blocking the main thread.

Example: Using await with an asynchronous method:

csharp

Copy public async Task GetDataAsync() { var result = await SomeAsyncMethod(); return result; } Save Definition: In the context of databases, Save typically refers to persisting changes to the database, such as saving new records or updating existing ones.

Example: Using Save in Entity Framework:

csharp

Copy using (var context = new MyDbContext()) { context.SaveChanges(); } SaveAsync Definition: Similar to Save, but used in asynchronous programming to save changes without blocking the main thread.

Example: Using SaveAsync in Entity Framework:

csharp

Copy using (var context = new MyDbContext()) { await context.SaveChangesAsync(); } These concepts are fundamental in modern programming, especially when dealing with I/O-bound operations and improving application responsiveness.