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 96 of 196
What is the difference between Last() and LastOrDefault() in Linq C#?
Both Last() and LastOrDefault() will fetch the last occurrence of a value. But the major difference between Last() and LastOrDefault() is that Last() will throw an exception if there is no result data for the supplied criteria whereas LastOrDefault() will return the default value (null) if there is no result data.Use Last() when we knew the sequence will have at least one element. Use LastOrDefault() when we are not sure about the data.Exampleusing System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; namespace ConsoleApp { public class Student { public int Id { get; set; } ...
Read MoreWhat if we are not sure of the type of value that we want to store in a variable. How to handle this in C#?
As C# is a strongly-typed language, every variable and constant has a pre-defined type. Before using any variable, we must tell the compiler what type of value a variable will store.If we are not sure about the type, then it is handled using dynamic programming. Dynamic programming is supported by the dynamic keyword.The dynamic keyword is used to declare dynamic types. The dynamic types tell the compiler that the object is defined as dynamic and skip type-checking at compiler time, delay type-checking until runtime. All syntaxes are checked and errors are thrown at runtime.Exampleusing System; namespace DemoDynamicKeyword{ class Program{ ...
Read MoreHow to store n number of lists of different types in a single generic list in C#?
We can store n number of lists of different types in a single generic list by creating a list of list of objects as shown below.List list = new List();Exampleusing System; using System.Collections.Generic; namespace MyApplication{ public class Program{ public static void Main(){ List list = new List(); List list1 = new List(); list1.Add(101); list1.Add(102); list1.Add(103); list.Add(list1); List list2 = new List(); list2.Add("Test1"); ...
Read MoreWhat is the difference between int and Int32 in C#?
Int32 is a type provided by .NET framework whereas int is an alias for Int32 in C# language.Int32 x = 5;int x = 5;So, in use both the above statements will hold a 32bit integer. They compile to the same code, so at execution time there is no difference whatsoever.The only minor difference is Int32 can be only used with System namespace. While validating the type of a value like mentioned above we can use Int32 or int.typeof(int) == typeof(Int32) == typeof(System.Int32)ExampleThe below example shows how an integer is declared using System.Int32.using System; namespace DemoApplication{ class Program{ ...
Read MoreWhat does the two question marks together (??) mean in C#?
It is the null-coalescing operator. The null-coalescing operator ?? returns the value of its left-hand operand if it isn't null; otherwise, it evaluates the right-hand operand and returns its result. The ?? operator doesn't evaluate its right-hand operand if the lefthand operand evaluates to non-null.A nullable type can represent a value that can be undefined or from the type's domain. We can use the ?? operator to return an appropriate value when the left operand has a nullable type. If we try to assign a nullable value type to a non-nullable value type without using the ?? operator, we will get ...
Read MoreWhat is @ in front of a string in C#?
It marks the string as a verbatim string literal.In C#, a verbatim string is created using a special symbol @. @ is known as a verbatim identifier. If a string contains @ as a prefix followed by double quotes, then compiler identifies that string as a verbatim string and compile that string. The main advantage of @ symbol is to tell the string constructor to ignore escape characters and line breaks.Exampleusing System; using System.IO; namespace DemoApplication{ class Program{ static void Main(string[] args){ Console.WriteLine("test string test string"); Console.WriteLine(@"test string ...
Read MoreWhich is better System.String or System.Text.StringBuilder classes in C#?
The main difference is StringBuilder is Mutable whereas String is Immutable.String is immutable, Immutable means if you create string object then you cannot modify it and It always create new object of string type in memory.On the other hand, StringBuilder is mutable. Means, if we create a string builder object then we can perform any operation like insert, replace or append without creating new instance for every time. It will update string at one place in memory doesn’t create new space in memory.Exampleusing System; using System.Text; class DemoApplication{ public static void Main(String[] args){ String systemString = ...
Read MoreWhat is the difference between List and IList in C#?
The main difference between List and IList in C# is that List is a class that represents a list of objects which can be accessed by index while IList is an interface that represents a collection of objects which can be accessed by index. The IList interface implemented from two interfaces and they are ICollection and IEnumerable.List and IList are used to denote a set of objects. They can store objects of integers, strings, etc. There are methods to insert, remove elements, search and sort elements of a List or IList. The major difference between List and IList is that ...
Read MoreWhat is an Optional parameter in C#?
By default, all parameters of a method are required. A method that contains optional parameters does not force to pass arguments at calling time. It means we call method without passing the arguments.The optional parameter contains a default value in function definition. If we do not pass optional argument value at calling time, the default value is used.Thera are different ways to make a parameter optional.Using Default ValueExampleusing System; namespace DemoApplication{ class Demo{ static void Main(string[] args){ OptionalMethodWithDefaultValue(5); //Value2 is not passed as it is optional ...
Read MoreWhat is the difference between Foreach and Parallel.Foreach in C#?
Foreach loop in C# runs upon a single thread and processing takes place sequentially one by one. Whereas Parallel.Foreach loop in C# runs upon multiple threads and processing takes place in a parallel way. Which means it is looping through all items at once without waiting for the previous item to complete.The execution of Parallel.Foreach is faster than normal ForEach. To use Parallel.ForEach loop we need to import System.Threading.Tasks namespace.Exampleusing System; using System.Collections.Generic; using System.Diagnostics; using System.Threading; using System.Threading.Tasks; namespace DemoApplication{ class Demo{ static void Main(string[] args){ var animals = new List{ ...
Read More