InternetException

Posts

What type can not be object field?

· 16 words · ~1 min read · in .NET, interview

ref struct type can not be used as object field. Further read: `ref struct` types https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/ref-struct

`ref struct` types

· 66 words · ~1 min read · in .NET, interview

added as part of C# 7.2. defined with ref struct StructName always on the stack never part of heap cannot be element of an array cannot be field type of reference type can’t implement interfaces can’t be boxed can’t be type argument can’t be captured by closure of lambda or local function can’t be used in async methods can’t be used in iterators Further read: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/ref-struct

Reference types vs Value types

· 41 words · ~1 min read · in .NET, interview

Reference types: passed via reference reside on the heap subject to GC inherits from System.Object Value types: passed via value reside on the stack, CPU registers or the heap no subject to GC inherits from System.ValueType Further read: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/reference-types https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-types https://www.infoworld.com/article/3043992/a-deep-dive-value-and-reference-types-in-net.html

.NET Type System

· 23 words · ~1 min read · in .NET, interview

shared type system across all .net languages primitives data types library 5 kind of types: classes structures enumerations interfaces delegates Further read: https://learn.microsoft.com/en-us/dotnet/standard/base-types/common-type-system

ThreadPool

· 22 words · ~1 min read · in .NET, interview

provides a pool of threads can be used to execute tasks post work items process asynchronous I/O process timers Further read: https://learn.microsoft.com/en-us/dotnet/api/system.threading.threadpool?view=net-6.0

Task Parallel Library (TPL)

· 23 words · ~1 min read · in .NET, interview

simplify process of adding parallelism and concurrency to application scales concurrency automatically data parallelism task based async programming data flow Further read: https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/task-parallel-library-tpl

Locking with integer

· 35 words · ~1 min read · in .NET, interview

In short: it will not work because it requires boxing and boxing will create new object for reference in lock every time code is executed so lock statement will not prevent anything. Further read: https://intellitect.com/blog/locking-a-block-of-code-with-an-integer/

Locking in .NET

· 141 words · ~1 min read · in .NET, interview

  • lock statement blocks execution of a code block for single thread that hold lock
  • the same thread can acquire and release lock again
  • any other thread is blocked from the statement
  • lock on dedicated instance only
  • using the same instance on multiple lock statements may lead to deadlock
  • avoid using as lock
    • this, as it might be used by the callers as a lock
    • Type instances, as those might be obtained by the typeof operator or reflection
    • string instances, including string literals, as those might be interned
  • Hold a lock for as short time as possible to reduce lock contention.

Below code block is translated by compiler

Read the whole thing →

Thread vs task in . NET

· 13 words · ~1 min read · in .NET, interview

task is asynchronous operation thread is abstraction on OS thread Further read: https://www.c-sharpcorner.com/article/task-and-thread-in-c-sharp/

What is SynchronizationContext in .NET

· 126 words · ~1 min read · in .NET, interview

  • multi threaded programs usually uses one of the threads as means of synchronization
  • there are message loops to pass unit of works (like Windows message queue)
  • message queues require messages formats and convention for handling it
  • single thread to handle ASP.NET request is waste of the resources (before .NET 2)
  • thread pool was better but inconvenient to work with
  • instead SynchronizationContext queues work for given context not thread
  • thread have current context
  • context does not have to be unique but can be shared between multiple threads
  • thread can change context but this is rare
  • SynchronizationContext keeps count of all asynchronous operations
  • Windows Forms uses WindowsFormsSynchronizationContext
  • WPF and Silverlight uses DispatcherSynchronizationContext
  • Console app and WindowsServices and ThreadPool threads uses default SynchronizationContext
  • ASP.NET uses AspNetSynchronizationContext

Further read:

Read the whole thing →

Does async/await creates new thread?

· 111 words · ~1 min read · in .NET, interview

#async

In short: no.

Longer explanation:

  • task that want to do I/O write call to driver write
  • driver operations are performed asynchronously
  • write operations on devices is done without CPU involved
  • finish of writing is done via interrupt
  • interrupt, schedules Deferred Procedure Call
  • DPC schedules Asynchronous Procedure Call
  • APC notifies thread that I/O write is done
  • task that was doing I/O is scheduled with continuation of its method after I/O is done.
  • some threads might need to be borrowed for handling APC and such but no thread is spawn to handle I/O

I recommend reading this post: https://blog.stephencleary.com/2013/11/there-is-no-thread.html . This will explain the topic much better than I can.

Read the whole thing →

Avoid deadlock with asynchronous methods

· 31 words · ~1 min read · in .NET, interview

#async

avoid doing AsyncOperation.Result parent method waits for Result while child method waits for parent thread to take control, after being done with async I/O both waits deadlock Further read: https://medium.com/rubrikkgroup/understanding-async-avoiding-deadlocks-e41f8f2c6f5d https://stackoverflow.com/questions/15021304/an-async-await-example-that-causes-a-deadlock

Asynchronous programming in .NET

· 46 words · ~1 min read · in .NET, interview

#async #design patterns

Asynchronous programming patterns in .NET: Asynchronous Programming Model (BeginOperation/EndOperation) Event-based Asynchronous Pattern (invoke event with result from async job) Task-based Asynchronous Pattern (Task<>) Ways of achieving asynchronous code in .NET async, await Task class API (Run, WhenAll, WhenAny, etc.) ThreadPool.QueueUserWorkItem(ThreadProc); Further read: async/await in .NET https://learn.microsoft.com/en-us/dotnet/standard/asynchronous-programming-patterns/

async/await in C#

· 119 words · ~1 min read · in .NET, interview

#async

async/await syntax should be used for I/O bound asynchronous operations only.

public async Task<int> GetUrlContentLengthAsync()
{
    var client = new HttpClient();

    Task<string> getStringTask =
        client.GetStringAsync("https://docs.microsoft.com/dotnet");

    DoIndependentWork();

    string contents = await getStringTask;

    return contents.Length;
}

Compiler transforms code into a state machine that to track yielding execution for await and resuming execution when a background job is finished.

Things to keep in mind:

  • async method needs await
  • add Async suffix to asynchronous methods
  • async void for ‘Fire and Forget’ work i.e. event Handlers
  • be aware of mixing LINQ with async lambdas
  • wait for tasks in a non-blocking manner (do not use Task.Result i.e.)
  • for tight loops use ValueTasks
  • Consider using ConfigureAwait(false) for performance and for avoiding deadlocks

Further read:

Read the whole thing →