Found 33676 Articles for Programming

How to use indexers in C# 8.0?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 14:11:26

117 Views

^ Operator − It is known as the index from the end operator.It returns an index that is relative to the end of the sequence or collection.It is the most compact and easiest way to find the end elements compare to earlier methods.company.listEmployees[^2].Name = "Employee 2 Name Changed using new Syntax";company.listEmployees[^5].Name = "Employee 5 Name Changed using new Syntax";company.listEmployees[^8].Name = "Employee 8 Name Changed using new Syntax";Examplepublic class Employee{    public int EmployeeId { get; set; }    public string Name { get; set; }    public string Gender { get; set; } } public class Company{    public List ... Read More

How to write the new Switch Expression in C# 8.0?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 14:08:02

1K+ Views

The switch expression provides for switch-like semantics in an expression contextswitch 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.ExampleNew way of Writing the Switchvar message = c switch{    Fruits.Red => "The Fruits is red",    Fruits.Green => "The Fruits is green",    Fruits.Blue => "The Fruits is blue" };Example 1class Program{    public enum Fruits { Red, Green, ... Read More

What are Ref locals and Ref returns in C# 7.0?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 14:05:34

641 Views

A reference return value allows a method to return a reference to a variable, rather than a value.The caller can then choose to treat the returned variable as if it were returned by value or by reference.The caller can create a new variable that is itself a reference to the returned value, called a ref local.In the below example even though we modify the color it doesn't have any mpact on the original array colorsExampleclass Program{    public static void Main(){       var colors = new[] { "blue", "green", "yellow", "orange", "pink" };       string color ... Read More

What are binary literals and digit separators in C# 7.0?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 13:59:34

428 Views

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 More

What is Pattern Matching in C# 7.0?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 13:52:35

331 Views

C# 7.0 introduces pattern matching in two cases, the is expression and the switch statement.Patterns test that a value has a certain shape, and can extract information from the value when it has the matching shape.Pattern matching provides more concise syntax for algorithmsYou can perform pattern matching on any data type, even your own, whereas with if/else, you always need primitives to match.Pattern matching can extract values from your expression.Before Pattern Matching −Examplepublic class PI{    public const float Pi = 3.142f; } public class Rectangle : PI{    public double Width { get; set; }    public double height ... Read More

What are Local functions in C# 7.0?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 13:48:35

188 Views

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

What are Deconstructors in C# 7.0?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 13:47:24

203 Views

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 More

What are the improvements in Out Parameter in C# 7.0?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 13:45:14

208 Views

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 More

What is difference between using if/else and switch-case in C#?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 13:43:48

465 Views

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 More

How to implement interface in anonymous class in C#?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 13:39:59

2K+ Views

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 More

Advertisements