Ankith Reddy has Published 1070 Articles

How to copy collection to Array using C#?

Ankith Reddy

Ankith Reddy

Updated on 21-Jun-2020 16:42:08

117 Views

To copy a collection to an array, firstly set it −List < string > list1 = new List < string > (); list1.Add("Car"); list1.Add("Bus"); list1.Add("Motorbike"); list1.Add("Train");Now declare a string array and use the CopyTo() method to copy −string[] arr = new string[20]; list1.CopyTo(arr);Let us see the complete code to copy ... Read More

Swap two numbers in C#

Ankith Reddy

Ankith Reddy

Updated on 21-Jun-2020 16:27:42

1K+ Views

To swap two numbers, work with the following logic.Set two variables for swapping −val1 = 100; val2 = 200;Now perform the following operation for swap −val1 = val1 + val2; val2 = val1 - val2; val1 = val1 - val2;The following is the code −Exampleusing System; namespace Demo {   ... Read More

Packages in C#

Ankith Reddy

Ankith Reddy

Updated on 21-Jun-2020 16:23:18

2K+ Views

As an alternative of Packages in Java, the C# language has namespace.Packages in JavaPackages are used in Java in order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier, etc.Namespace in C#A namespace is designed for providing a way to ... Read More

Tasks in C#

Ankith Reddy

Ankith Reddy

Updated on 21-Jun-2020 16:14:47

3K+ Views

Task represents an asynchronous operation in C#. The following states how you can start a task in C#.Use a delegate to start a task.Task t = new Task(delegate { PrintMessage(); }); t.Start();Use Task Factory to start a task.Task.Factory.StartNew(() => {Console.WriteLine("Welcome!"); });You can also use Lambda.Task t = new Task( () ... Read More

Naming Conventions in C#

Ankith Reddy

Ankith Reddy

Updated on 21-Jun-2020 16:08:58

2K+ Views

Naming convetion for classesA class definition starts with the keyword class followed by the class name; and the class body enclosed by a pair of curly braces. The following are the conventions for class names.Pascal CasingThe coding conventions for a class name is the the name of the class names, ... Read More

Operator Functions in C#

Ankith Reddy

Ankith Reddy

Updated on 21-Jun-2020 15:59:26

137 Views

Operator functions are overloaded operator, which are the functions with special names. To create it, the keyword operator is followed by the symbol for the operator being defined.Like any other function, an overloaded operator has a return type and a parameter list.For example −public static Box operator+ (Vehicle v1, Vehicle ... Read More

Initializing HashSet in C#

Ankith Reddy

Ankith Reddy

Updated on 21-Jun-2020 15:50:18

605 Views

To initialize a HashSet.var h = new HashSet(arr1);Above, we have set an array in the HashSet. The following is the array −string[] arr1 = {    "electronics",    "accessories”,    "electronics", };The following is an example showing how to implement HashSet in C# −Exampleusing System; using System.Collections.Generic; using System.Linq; ... Read More

Reverse an array using C#

Ankith Reddy

Ankith Reddy

Updated on 21-Jun-2020 15:23:20

458 Views

Firstly, set the original array −int[] arr = { 1, 2, 3 }; // Original Array Console.WriteLine("Original Array= "); fo            reach (int i in arr) {    Console.WriteLine(i); }Now, use the Array.reverse() method to reverse the array −Array.Reverse(arr);The following is the complete code to ... Read More

What is Cast Operator () in C#?

Ankith Reddy

Ankith Reddy

Updated on 21-Jun-2020 15:03:46

584 Views

Type conversion is converting one type of data to another type. Explicit conversions are done explicitly by users using the pre-defined functions and require a cast operator.Let us see an example to cast double to int −Exampleusing System; namespace Demo {    class Program {     ... Read More

How to add items/elements to an existing jagged array in C#?

Ankith Reddy

Ankith Reddy

Updated on 21-Jun-2020 14:50:24

584 Views

To add an element to existing jagged array, just set the value of the element with a new value.Let’s say you need to add an element at the following location −a[3][1]Just set the value −a[3][1] = 500;Above, we accessed the first element of the 3rd array in a jagged array.Let ... Read More

Advertisements