Skip to content

Instantly share code, notes, and snippets.

View admir-live's full-sized avatar

Admir Mujkic admir-live

View GitHub Profile
@admir-live
admir-live / Program.cs
Created December 4, 2022 20:47
Program.cs
static class Program
{
private static void Main(string[] args)
{
const int suma = 4 + 5;
Console.WriteLine(suma);
}
}
@admir-live
admir-live / Example.cs
Created June 20, 2023 05:48
Example.cs
/// <summary>
/// Base class for car objects
/// </summary>
public abstract class Car
{
public virtual void StartEngine()
{
Console.WriteLine(value: "Car engine has been started.");
}
@admir-live
admir-live / StreamSyncService.cs
Created August 29, 2023 10:28
StreamSyncService.cs
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
@admir-live
admir-live / ResponseHeadersRead.cs
Created August 29, 2023 13:05
The key is to read the content and dispose of it properly.
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)
{
@admir-live
admir-live / GlobalHttpExceptionFilter.cs
Created August 30, 2023 09:18
Example of GlobalHttpExceptionFilter with BadRequest
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>>
using System;
using System.Threading;
public class ThreadPoolExample
{
public static void Main()
{
// Queue a task.
ThreadPool.QueueUserWorkItem(DoWork, "Task 1");
@admir-live
admir-live / AsyncExamples.cs
Created September 6, 2023 06:12
AsyncExamples.cs
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();
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!");
}
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!";
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
IAsyncEnumerable<int> numbers = GetNumbersAsync();