This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
const int suma = 4 + 5; | |
Console.WriteLine(suma); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Base class for car objects | |
/// </summary> | |
public abstract class Car | |
{ | |
public virtual void StartEngine() | |
{ | |
Console.WriteLine(value: "Car engine has been started."); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed class StreamSyncService : BackgroundService, IStreamSyncService | |
{ | |
// Class Definition and Properties | |
private const int DegreeOfParallelism = 100; | |
// We have to see if we have api limits per social media network. In case if we have we have to create channel per network type. | |
private readonly Channel<SocialMediaPost> _channel; | |
private readonly ILogger<StreamSyncService> _logger; | |
private readonly IServiceProvider _serviceProvider; | |
// Constructor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
public class Program | |
{ | |
private static readonly HttpClient client = new HttpClient(); | |
public static async Task Main(string[] args) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GlobalHttpExceptionFilter : ExceptionFilterAttribute | |
{ | |
private readonly ILogger<GlobalHttpExceptionFilter> _exceptionLogger; | |
private readonly Dictionary<Type, Action<ExceptionContext, Exception>> _exceptionHandlers; | |
public GlobalHttpExceptionFilter(ILogger<GlobalHttpExceptionFilter> exceptionLogger) | |
{ | |
_exceptionLogger = exceptionLogger ?? throw new ArgumentNullException(nameof(exceptionLogger)); | |
_exceptionHandlers = new Dictionary<Type, Action<ExceptionContext, Exception>> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
public class ThreadPoolExample | |
{ | |
public static void Main() | |
{ | |
// Queue a task. | |
ThreadPool.QueueUserWorkItem(DoWork, "Task 1"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Net.Http; | |
using System.Threading.Tasks; | |
public class AsyncExamples | |
{ | |
// GOOD EXAMPLE | |
// This method performs an asynchronous operation and hence uses async and await appropriately. | |
public async Task<string> GetDataAsync() | |
{ | |
HttpClient client = new HttpClient(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Threading.Tasks; | |
public class TaskExamples | |
{ | |
// Using Task when not awaiting and returning task-like result | |
public Task<string> GetStaticMessageTask() | |
{ | |
// Directly returning a completed task without the need for async keyword | |
return Task.FromResult("Hello, World!"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StateMachineExamples | |
{ | |
// Inefficient use of async | |
public async Task<string> InefficientMethodAsync() | |
{ | |
// This method is marked as async, but it lacks any await statements. | |
// As a result, the compiler generates a state machine which is unnecessary. | |
return "Hello, World!"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
class Program | |
{ | |
static async Task Main() | |
{ | |
IAsyncEnumerable<int> numbers = GetNumbersAsync(); |
OlderNewer