Posts

Showing posts from October, 2019

How Dependency Injection Containers Work in C#?

Dependency Injection (DI) containers, such as Unity or DryIoc, help manage the creation and lifetime of object dependencies in C#. They facilitate the Inversion of Control (IoC) principle, allowing you to focus on writing clean, maintainable code without worrying about the complexities of instantiating dependencies manually. How DI Containers Work? Registration:  You define which concrete classes should be used to fulfill specific interface contracts. This allows the DI container to know what to instantiate when a class requests a particular dependency. Resolution:  When an instance of a class is requested, the DI container looks at the registered services, resolves the dependencies, and creates the object with the required dependencies injected. Lifetime Management:  The container manages the lifecycle of the dependencies. You can specify whether instances should be singleton (one instance for the entire application), transient (a new instance each time), or scoped (one ...

How to create TextBox in wpf that does not accept number using MVVM?

Image
Method 1: Using Model Property Inside View <TextBox Width="200" Text="{Binding MainWindowModel.NonNumeric,UpdateSourceTrigger=PropertyChanged}"> Inside Model  private string _nonNumeric;         public string NonNumeric         {             get { return _nonNumeric; }             set             {                 int intvalue;                 if (!int.TryParse(value[value.Length - 1].ToString(), out intvalue))                     SetProperty(ref _nonNumeric, value);                 else                     value = _nonNumeric;             }         } M...

How to create Own Semaphore?

 class CustomSemaphore     {         private Mutex[] arrMutex;         // place holder to store thread to mutex mapping         private Thread[] arrthread;         // number of threads allowed to access the resources         private int threadLimit;         // contructor creates the mutexes         public CustomSemaphore(int threadLimit)         {             this.threadLimit = threadLimit;             arrMutex = new Mutex[this.threadLimit];             arrthread = new Thread[this.threadLimit];             for (int i = 0; i < this.threadLimit; i++)             {                 arrMutex[i] = new Mutex()...

Write a method for division of two number without using division (‘/’) operator in C#.

    void DivideWithoutDivisionOperator(int divident,int divisior)         {             int quotient = 0;             bool isNegative = false;             if (divident < 0)//if negative             {                 divident = -divident;//make positive                 if (divisior < 0)//if negative                     divisior = -divisior;//make positive                 else                     isNegative = true;             }             else if (divisior < 0)//if negative             {      ...