Found 35164 Articles for Programming

Intersect two lists in C#

George John
Updated on 22-Jun-2020 15:51:21

6K+ Views

Firstly, set two lists.List val1 = new List { 25, 30, 40, 60, 80, 95, 110 }; List val2 = new List { 27, 35, 40, 75, 95, 100, 110 };Now, use the Intersect() method to get the intersection between two lists.IEnumerable res = val1.AsQueryable().Intersect(val2);Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       List val1 = new List { 25, 30, 40, 60, 80, 95, 110 };       List val2 = new List { 27, 35, 40, 75, 95, 100, 110 };       IEnumerable res = val1.AsQueryable().Intersect(val2);       Console.WriteLine("Intersection of both the lists...");       foreach (int a in res) {          Console.WriteLine(a);       }    } }OutputIntersection of both the lists... 40 95 110

Find missing number in a sequence in C#

Ankith Reddy
Updated on 22-Jun-2020 15:33:16

716 Views

Set a list.List myList = new List(){1, 2, 3, 5, 8, 9};Now, get the first and last elements −int a = myList.OrderBy(x => x).First(); int b = myList.OrderBy(x => x).Last();In a new list, get all the elements and use the Except to get the missing numbers −List myList2 = Enumerable.Range(a, b - a + 1).ToList(); List remaining = myList2.Except(myList).ToList();Let us see the complete code −Example Live Demousing System.Collections.Generic; using System; using System.Linq; public class Program {    public static void Main() {       List myList = new List(){1, 2, 3, 5, 8, 9};       Console.WriteLine("Numbers... "); ... Read More

What is the operator that concatenates two or more string objects in C#?

Arjun Thakur
Updated on 22-Jun-2020 15:34:05

549 Views

Use operator + to concatenate two or more string objects.Set the first string object.char[] c1 = { 'H', 'e', 'n', 'r', 'y' }; string str1 = new string(c1);Now, set the second string object.char[] c2 = { 'J', 'a', 'c', 'k' }; string str2 = new string(c2);Now display the concatenated strings using + operator.Example Live Demousing System.Text; using System; class Program {    static void Main() {       char[] c1 = { 'H', 'e', 'n', 'r', 'y' };       string str1 = new string(c1);       char[] c2 = { 'J', 'a', 'c', 'k' };       string str2 = new string(c2);       Console.WriteLine("Welcome " + str1 + " and " + str2 + "!");    } }OutputWelcome Henry and Jack!

Where to use #region directive in C#?

Chandu yadav
Updated on 22-Jun-2020 15:34:39

4K+ Views

It lets you specify a block of code that you can expand or collapse when using the outlining feature of the Visual Studio Code Editor. It should be terminated with #endregion.Let us see how to define a region using #region.#region NewClass definition public class NewClass {    static void Main() { } } #endregionThe following is an example showing the usage of #region directive.Example Live Demousing System; #region class MyClass { } #endregion class Demo {    #region VARIABLE    int a;    #endregion    static void Main() {       #region BODY       Console.WriteLine("Example showing the usage of region directive!");       #endregion    } }OutputExample showing the usage of region directive!

Why is a Dictionary preferred over a Hashtable in C#?

George John
Updated on 22-Jun-2020 15:36:35

162 Views

Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. It uses the key to access the elements in the collection.Dictionary is a collection of keys and values in C#. Dictionary is included in the System.Collection.Generics namespace.Hashtable is slower than Dictionary. For strongly-typed collections, the Dictionary collection is faster.Let’s say we need to find a key from Hashtable collections. With that, we are also finding a key from Dictionary collection as well. In that case, Dictionary would be faster for the same statement −For HashTablehashtable.ContainsKey("12345");For Dictionarydictionary.ContainsKey("12345") Read More

C# program to create a ValueType with names

Ankith Reddy
Updated on 22-Jun-2020 15:35:11

97 Views

With C# 7, you can easily create a ValueType with names.Note − Add System.ValueTuple package to run ValueTuple program.Let’s see how to add it −Go to your projectRight click on the project in the solution explorerSelect “Manage NuGet Packages”You will reach the NuGet Package Manager.Now, click the Browse tab and find “ValueTuple”Finally, add System.ValueTuple packageExampleusing System; class Program {    static void Main() {       var myTuple = (marks: 95, name: "jack", subject: "maths");       //Add System.ValueTuple package to run this program       // using names to access       Console.WriteLine("Student Marks: "+myTuple.marks); ... Read More

How to generate a string randomly using C#?

Arjun Thakur
Updated on 22-Jun-2020 15:37:33

461 Views

Firstly, set a string.StringBuilder str = new StringBuilder();Use Random.Random random = new Random((int)DateTime.Now.Ticks);Now loop through a number which is the length of the random string you want.for (int i = 0; i < 4; i++) {    c = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));    str.Append(c); }On every iteration above, a random character is generated and appended to form a string.The following is the complete example −Example Live Demousing System.Text; using System; class Program {    static void Main() {       StringBuilder str = new StringBuilder();       char c;       Random random = new Random((int)DateTime.Now.Ticks); ... Read More

C# Hexadecimal ("X") Format Specifier

Samual Sam
Updated on 22-Jun-2020 15:39:09

6K+ Views

The hexadecimal ("X") format specifier is used to convert a number to a string of hexadecimal digits.Set the case of the format specifier for uppercase or lowercase characters to be worked on hexadecimal digits greater than 9.Let us understand this with an example −“X” for PQR, whereas “x” for pqrExample Live Demousing System; using System.Numerics; using System.Globalization; class Demo {    static void Main() {       int num;       num = 345672832;       Console.WriteLine(num.ToString("X"));       Console.WriteLine(num.ToString("X2"));       num = 0x307e;       Console.WriteLine(num.ToString("x"));       Console.WriteLine(num.ToString("X"));    } }Output149A8C80 149A8C80 307e 307E

C# Round-trip ("R") Format Specifier

karthikeya Boyini
Updated on 22-Jun-2020 15:39:31

556 Views

This round-trip ("R") format specifier is supported for the Single, Double, and BigInteger types.It ensures that a numeric value converted to a string is parsed back into the same numeric value.Let us see an example −Firstly, we have a double variable.double doubleVal = 0.91234582637;Now, use the ToString() method: and set the Round-trip format specifier.doubleVal.ToString("R", CultureInfo.InvariantCulture);Let us see the complete example −Example Live Demousing System; using System.Numerics; using System.Globalization; class Demo {    static void Main() {       double doubleVal = 0.91234582637;       string str = doubleVal.ToString("R", CultureInfo.InvariantCulture);       double resRound = double.Parse(str, CultureInfo.InvariantCulture);     ... Read More

Convert a ValueTuple to a Tuple in C#

Samual Sam
Updated on 22-Jun-2020 15:27:27

336 Views

With C#, we can easily convert a ValueTuple to a Tuple using ToTuple() method.Note − Add System.ValueTuple package to run ValueTuple program.Let’s see how to add it −Go to your projectRight click on the project in the solution explorerSelect “Manage NuGet Packages”You will reach the NuGet Package Manager.Now, click the Browse tab and find “ValueTuple”Finally, add System.ValueTuple packageExampleusing System; class Program {    static void Main() {       var val = (5, 50, 500, 5000);       //Add System.ValueTuple package to run this program       // ValueTuple       Console.WriteLine(“ValueTuple: ” val);       // Tuple       Tuple myTuple = val.ToTuple();       Console.WriteLine(“Tuple: ”+myTuple);    } }OutputValueTuple: (5, 50, 500, 5000) Tuple: (5, 50, 500, 5000)

Advertisements