Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles by Nizamuddin Siddiqui
Page 172 of 196
How to write retry logic in C#?
Retry logic is implemented whenever there is a failing operation. Implement retry logic only where the full context of a failing operation.It's important to log all connectivity failures that cause a retry so that underlying problems with the application, services, or resources can be identified.Exampleclass Program{ public static void Main(){ HttpClient client = new HttpClient(); dynamic res = null; var retryAttempts = 3; var delay = TimeSpan.FromSeconds(2); RetryHelper.Retry(retryAttempts, delay, () =>{ res = client.GetAsync("https://example22.com/api/cycles/1"); }); ...
Read MoreWhat is the difference between Monitor and Lock in C#?
Both Monitor and lock provides a mechanism that synchronizes access to objects. lock is the shortcut for Monitor.Enter with try and finally.Lock is a shortcut and it's the option for the basic usage. If we need more control to implement advanced multithreading solutions using TryEnter() Wait(), Pulse(), & PulseAll() methods, then the Montior class is your option.Example for Lock −Exampleclass Program{ static object _lock = new object(); static int Total; public static void Main(){ AddOneHundredLock(); Console.ReadLine(); } public static void AddOneHundredLock(){ for (int i = 1; i
Read MoreHow to sort a list of complex types using Comparison delegate in C#?
Overloads of the Sort() method in List class expects Comparison delegate to be passed as an argument.public void Sort(Comparison comparison)CompareTo returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the specified object or the other Int16 instance.The Int16.CompareTo() method in C# is used to compare this instance to a specified object or another Int16 instanceExampleclass Program{ public static void Main(){ Employee Employee1 = new Employee(){ ID = 101, Name = "Mark", ...
Read MoreWhat is if/then directives for debug vs release in C#?
In Visual Studio Debug mode and Release mode are different configurations for building your .Net project.Select the Debug mode for debugging step by step their .Net project and select the Release mode for the final build of Assembly file (.dll or .exe).To change the build configuration −From the Build menu, select Configuration Manager, then select Debug or Release. Or On the toolbar, choose either Debug or Release from the Solution Configurations list.The code which is written inside the #if debug will be executed only if the code is running inside the debug mode.If the code is running in the release ...
Read MoreHow to get the Unix timestamp in C#
A Unix timestamp is mainly used in Unix operating systems. But it is helpful for all operating systems because it represents the time of all time zones.Unix Timestamps represent the time in seconds. The Unix epoch started on 1st January 1970.So, Unix Timestamp is the number of seconds between a specific dateExampleto get the Unix Timestamp Using DateTime.Now.Subtract().TotalSeconds Methodclass Program{ static void Main(string[] args){ Int32 unixTimestamp = (Int32)(DateTime.Now.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; Console.WriteLine("The Unix Timestamp is {0}", unixTimestamp); Console.ReadLine(); } }Output1596837896Exampleto get the Unix Timestamp Using DateTimeOffset.Now.ToUnixTimeSeconds() ...
Read MoreHow to force garbage collection in C#?
Yes it is possible to force garbage collector in C# to run by calling Collect() methodThis is not considered a good practice because this might create a performance over head. Collect() Forces an immediate garbage collection of all generations.Collect(Int32)Forces an immediate garbage collection from generation 0 through a specified generation.Example Live Demousing System; class MyGCCollectClass{ private const int maxGarbage = 1000; static void Main(){ // Put some objects in memory. MyGCCollectClass.MakeSomeGarbage(); Console.WriteLine("Memory used before collection: {0:N0}", GC.GetTotalMemory(false)); // Collect all generations of memory. ...
Read MoreHow to create an array with non-default repeated values in C#?
We can create an array with non-default values using Enumerable.Repeat(). It repeated a collection with repeated elements in C#. Firstly, set which element you want to repeat and how many times.Example 1class Program{ static void Main(string[] args){ var values = Enumerable.Repeat(10, 5); foreach (var item in values){ System.Console.WriteLine(item); } Console.ReadLine(); } }Output10 10 10 10 10Example 2class Program{ static void Main(string[] args){ int[] values = Enumerable.Repeat(10, 5).ToArray(); foreach (var item in values){ System.Console.WriteLine(item); } Console.ReadLine(); } }Output10 10 10 10 10
Read MoreHow to make use of both Take and Skip operator together in LINQ C#?
The Take operator is used to return a given number of elements from an array and the Skip operator skips over a specified number of elements from an array.Skip, skips elements up to a specified position starting from the first element in a sequence.Take, takes elements up to a specified position starting from the first element in a sequence.Example 1class Program{ static void Main(string[] args){ List numbers = new List { 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 5, 6, 7, ...
Read MoreWhat is Shallow Copy and how it is different from Deep Copy in C#?
Shallow Copy −A shallow copy of an object copies the "main" object, but doesn’t copy the inner objects.The "inner objects" are shared between the original object and its copy.The problem with the shallow copy is that the two objects are not independent. If you modify the one object, the change will be reflected in the other object.Deep Copy −A deep copy is a fully independent copy of an object. If we copied our object, we would copy the entire object structure.If you modify the one object, the change will not be reflected in the other object.Exampleclass Program{ static void ...
Read MoreHow can we create a LOG filter for Logging purposes in C# ASP.NET WebAPI?
Action filters are used to add extra logic before or after action methods execution. The OnActionExecuting and OnActionExecuted methods are used to add our logic before and after an action method is executed.Let us create create a LogAttribute that implemets ActionFilterAttribute which logs some information before and after action method execution.LogAttribute −Exampleusing System; using System.Diagnostics; using System.Web.Http.Controllers; using System.Web.Http.Filters; namespace DemoWebApplication.Controllers{ public class LogAttribute : ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext actionContext){ Debug.WriteLine(string.Format("Action Method {0} executing at {1}", actionContext.ActionDescriptor.ActionName, DateTime.Now.ToShortDateString()), ...
Read More