Sort an Array in Descending Order Using Chash

karthikeya Boyini
Updated on 22-Jun-2020 10:25:12

461 Views

Declare an array and initialize −int[] arr = new int[] {    87,    23,    65,    29,    67 };To sort, use the Sort() method and CompareTo() to compare and display in decreasing order −Array.Sort < int > (arr, new Comparison < int > ((val1, val2) => val2.CompareTo(val1)));Let us see the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Text; public class Demo {    public static void Main(string[] args) {       int[] arr = new int[] {          87,          23,          65,     ... Read More

Sort Words in Lexicographical Order in C#

Arjun Thakur
Updated on 22-Jun-2020 10:24:33

1K+ Views

Firstly, set a string array −string[] arr = new string[] {    "Indian",    "Moroccon",    "American", };To sort the words in lexicographical order −var sort = from a in arr orderby a select a;Example Live DemoLet us see the complete code −using System; using System.Linq; class Program {    static void Main() {       string[] arr = new string[] {          "Indian",          "Moroccon",          "American",       };       var sort = from a in arr       orderby a       select a;       foreach(string res in sort) {          Console.WriteLine(res);       }    } }outputAmerican Indian Moroccon

SortedMap Interface in C#

Samual Sam
Updated on 22-Jun-2020 10:23:59

562 Views

Java has SortedMap Interface, whereas an equivalent of it in C# is SortedList.SortedList collection in C# use a key as well as an index to access the items in a list.A sorted list is a combination of an array and a hash table. It contains a list of items that can be accessed using a key or an index. If you access items using an index, it is an ArrayList, and if you access items using a key, it is a Hashtable. The collection of items is always sorted by the key value.Let us see an example to work with ... Read More

Date Format Validation Using C# Regex

karthikeya Boyini
Updated on 22-Jun-2020 10:23:28

16K+ Views

Use the DateTime.TryParseExact method in C# for Date Format validation.They method converts the specified string representation of a date and time to its DateTime equivalent. It checks whether the entered date format is correct or not.Example Live Demousing System; using System.Globalization; namespace Demo {    class Program {       static void Main(string[] args) {          DateTime d;          bool chValidity = DateTime.TryParseExact(          "08/14/2018",          "MM/dd/yyyy",          CultureInfo.InvariantCulture,          DateTimeStyles.None,          out d);          Console.WriteLine(chValidity);       }    } }OutputTrue

List the Difference Between Two Lists in C#

Samual Sam
Updated on 22-Jun-2020 10:23:00

2K+ Views

To get the difference between two lists, firstly set two lists in C# −// first list List < string > list1 = new List < string > (); list1.Add("A"); list1.Add("B"); list1.Add("C"); list1.Add("D"); // second list List < string > list2 = new List < string > (); list2.Add("C"); list2.Add("D"); foreach(string value in list2) {    Console.WriteLine(value); }To get the difference, use IEnumerable and Except() as shown below. The difference is shown in the third list −IEnumerable < string > list3; list3 = list1.Except(list2);The following is the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Demo ... Read More

Merge Two Sorted Arrays into One in C#

karthikeya Boyini
Updated on 22-Jun-2020 10:22:04

2K+ Views

Set two arrays that you wish to merge −int[] arr1 = new int[5] {    5,    15,    25,    30,    47 }; int[] arr2 = new int[5] {    55,    60,    76,    83,    95 };Now take a third array that would merge both the above arrays −int[] merged = new int[10];The following is the code that merges two arrays into the third array in C# −Example Live Demousing System; using System.Collections.Generic; class Program {    static void Main() {       int i = 0;       int j = 0; ... Read More

Type of Elements in Jagged Array in C#

Samual Sam
Updated on 22-Jun-2020 10:21:04

190 Views

A jagged array is an array of arrays, and therefore its elements are reference types and are initialized to null.Let us see how to work with Jagged array −Declare a jagged array −int [][] marks;Now, let us initialize it, wherein marks is an arrays of 5 integers −int[][] marks = new int[][]{new int[]{ 40, 57 }, new int[]{ 34, 55 }, new int[]{ 23, 44 }, new int[]{ 56, 78 }, new int[]{ 66, 79 } };Let us now see the complete example of jagged arrays in C# and learn how to implement it −Example Live Demousing System; namespace MyApplication ... Read More

What is Unboxing in C#

karthikeya Boyini
Updated on 22-Jun-2020 10:19:54

237 Views

Boxing is implicit and unboxing is explicit. Unboxing is the explicit conversion of the reference type created by boxing, back to a value type.Let us see an example of variable and object in C# −// int int x = 30; // Boxing object obj = x; // Un boxing int unboxInt = (int) obj;The following is an example showing Un boxing −int x = 5; ArrayList arrList = new ArrayList(); // Boxing arrList.Add(x); // UnBoxing int y = (int) arrList [0];

What is Unmanaged Code in C#

Samual Sam
Updated on 22-Jun-2020 10:19:37

680 Views

The following states what is an unmanaged code −Applications that are not under the control of the CLR are unmanagedThe unsafe code or the unmanaged code is a code block that uses a pointer variable.The unsafe modifier allows pointer usage in unmanaged code.Here is the module showing how to declare and use a pointer variable. We have used the unsafe modifier here.Let us see the example −Examplestatic unsafe void Main(string[] args) {    int var = 20;    int* p = &var;    Console.WriteLine("Data is: {0} ", var);    Console.WriteLine("Address is: {0}", (int)p);    Console.ReadKey(); }

C# Program to Convert Floating to Binary

Samual Sam
Updated on 22-Jun-2020 10:19:09

824 Views

Let’s say the following is our float −float n = 50.5f;Take an empty string to display the binary value and loop until the value of our float variable is greater than 1 −string a = ""; while (n >= 1) {    a = (n % 2) + a;    n = n / 2; }Let us see the complete example −Example Live Demousing System; using System.IO; using System.CodeDom.Compiler; namespace Program {    class Demo {       static void Main(string[] args) {          // float to binary          Console.WriteLine("float to binary = ");          float n = 50.5f;          string a = "";          while (n >= 1) {             a = (n % 2) + a;             n = n / 2;          }          Console.Write(a);       }    } }Outputfloat to binary = 1.5781251.156250.31250.6251.250.5

Advertisements