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 176 of 196
What are binary literals and digit separators in C# 7.0?
Binary Literals −Before C# 7 we were able to assign only decimal and hexadecimal values to a variable.In C# 7.0 binary literal has been introduced and it allows us binary value to the variable.Digit Separator −Digit Separator takes the form of a single underscore (_). This separator can be used within any numeric literal as a means of improving legibility.Example Binary Literals −Exampleclass Program{ public static void Main(){ var bn = 0b1000001; System.Console.WriteLine(bn.GetType()); System.Console.WriteLine(Convert.ToChar(bn)); Console.ReadLine(); } }OutputSystem.Int32 AExample Digit Seperator −Exampleclass Program{ public static void ...
Read MoreWhat are Local functions in C# 7.0?
Local functions are private methods of a type that are nested in another member. They can only be called from their containing member.Local functions can be declared in and called from −Methods, especially iterator methods and async methodsConstructorsProperty accessorsEvent accessorsAnonymous methodsLambda expressionsFinalizersOther local functionsExample 1class Program{ public static void Main(){ void addTwoNumbers(int a, int b){ System.Console.WriteLine(a + b); } addTwoNumbers(1, 2); Console.ReadLine(); } }Output3Example 2class Program{ public static void Main(){ void addTwoNumbers(int a, int b, out int c){ c = a + b; } addTwoNumbers(1, 2, out int c); System.Console.WriteLine(c); Console.ReadLine(); } }Output3
Read MoreWhat are Deconstructors in C# 7.0?
C# allows to use multiple deconstructor methods in the same program with the same number of out parameters or the same number and type of out parameters in a different order.It's a part of the new tuple syntax - which has nothing to do with the Tuple classes but is taking from functional programming.Deconstruct keyword is used for DeconstructorsExamplepublic class Employee{ public Employee(string employeename, string firstName, string lastName){ Employeename = employeename; FirstName = firstName; LastName = lastName; } public string Employeename { get; } public string FirstName ...
Read MoreWhat are the improvements in Out Parameter in C# 7.0?
We can declare out values inline as arguments to the method where they're used.The existing out parameters has been improved in this version. Now we can declare out variables in the argument list of a method call, rather than writing a separate declaration statement.Advantages −The code is easier to read.No need to assign an initial value.Existing Syntax −Exampleclass Program{ public static void AddMultiplyValues(int a, int b, out int c, out int d){ c = a + b; d = a * b; } public static void Main(){ int ...
Read MoreWhat is difference between using if/else and switch-case in C#?
Switch is a selection statement that chooses a single switch section to execute from a list of candidates based on a pattern match with the match expression.The switch statement is often used as an alternative to an if-else construct if a single expression is tested against three or more conditions.Switch statement is quicker. The switch statement the average number of comparisons will be one regardless of how many different cases you have So lookup of an arbitrary case is O(1)Using Switch −Exampleclass Program{ public enum Fruits { Red, Green, Blue } public static void Main(){ Fruits c = (Fruits)(new Random()).Next(0, ...
Read MoreHow to implement interface in anonymous class in C#?
No, anonymous types cannot implement an interface. We need to create your own type.Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first.The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler.You create anonymous types by using the new operator together with an object initializer.Exampleclass Program{ public static void Main(){ var v = new { Amount = 108, Message = "Test" }; ...
Read MoreHow 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 More