Nizamuddin Siddiqui

Nizamuddin Siddiqui

1,958 Articles Published

Articles by Nizamuddin Siddiqui

Page 191 of 196

How do you give a C# Auto-Property a default value?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Aug-2020 2K+ Views

In C# 5.0 and before to give an value to the Auto Property we have to do in the constructorThe constructor will be automatically called when class is instantiated and the value will be setAfter C#5.0 a new way to give a value to auto property has come which is similar in assigning a value to the variableExampleSet Value in Constructor class Demo{    public Demo(){       FirstName = "DemoName";    }    public string FirstName { get; set; } } class Program{    static void Main(){       Demo obj = new Demo();       ...

Read More

How can we return multiple values from a function in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Aug-2020 3K+ Views

In c# multiple values can be returned using the below approaches −Reference parametersOutput parametersReturning an ArrayReturning a TupleReference parametersExampleclass Program{    static int ReturnMultipleValuesUsingRef(int firstNumber, ref int secondNumber){       secondNumber = 20;       return firstNumber;    }    static void Main(){       int a = 10;       int refValue = 0;       var res = ReturnMultipleValuesUsingRef(a, ref refValue);       System.Console.WriteLine($" Ref Value {refValue}");       System.Console.WriteLine($" Function Return Value {res}");       Console.ReadLine();    } }OutputRef Value 20 Function Return Value 10Output parametersExampleclass Program{    static ...

Read More

Why cannot we specify access modifiers inside an interface in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Aug-2020 948 Views

Interface methods are contract with the outside world which specifies that class implementing this interface does a certain set of things.Interface members are always public because the purpose of an interface is to enable other types to access a class or struct.Interfaces can have access specifiers like protected or internal etc. Thus limiting 'the outside world' to a subset of 'the whole outside world'.Exampleinterface IInterface{    void Save(); } class Program{    static void Main(){       Console.ReadLine();    } }The above example will compile properly without any errorsPrior to C# 8, interface members were public by default. In ...

Read More

Which is better System.String or System.Text.StringBuilder classes in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Aug-2020 2K+ Views

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.Example Live Demousing System; using System.Text; class DemoApplication{    public static void Main(String[] args){       String systemString ...

Read More

What is @ in front of a string in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Aug-2020 14K+ Views

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.Example Live Demousing System; using System.IO; namespace DemoApplication{    class Program{       static void Main(string[] args){          Console.WriteLine("test string test string");          Console.WriteLine(@"test ...

Read More

What does the two question marks together (??) mean in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Aug-2020 3K+ Views

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 More

What is the difference between int and Int32 in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Aug-2020 6K+ Views

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. Live Demousing System; namespace DemoApplication{    class Program{     ...

Read More

How to store n number of lists of different types in a single generic list in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Aug-2020 839 Views

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();Example Live Demousing 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 More

What is the use of "is" keyword in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Aug-2020 416 Views

The "is" keyword is used to check if an object can be casted to a specific type. The return type of the operation is Boolean.Exampleusing System; namespace DemoApplication{    class Program{       static void Main(){          Employee emp = new PermanentEmployee{             ID = 1,             Name = "Martin"          };          // Returns true as the derived type can be converted to base type.          if (emp is Employee){             ...

Read More

What if we are not sure of the type of value that we want to store in a variable. How to handle this in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Aug-2020 114 Views

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.Example Live Demousing System; namespace DemoDynamicKeyword{    class ...

Read More
Showing 1901–1910 of 1,958 articles
« Prev 1 189 190 191 192 193 196 Next »
Advertisements