Found 2587 Articles for Csharp

What is the difference between function overriding and method hiding in C#?

Samual Sam
Updated on 21-Jun-2020 13:36:43

679 Views

OverridingUnder overriding, you can define a behavior that's specific to the subclass type, which means a subclass can implement a parent class method based on its requirement.Let us see an example of abstract classes that implements Overriding −Exampleusing System; namespace PolymorphismApplication {    abstract class Shape {       public abstract int area();    }    class Rectangle: Shape {       private int length;       private int width;       public Rectangle( int a = 0, int b = 0) {          length = a;       ... Read More

How to copy a section of an array into another array in C#?

Chandu yadav
Updated on 21-Jun-2020 13:38:56

297 Views

The Array.Copy() method in C# is used to copy section of one array to another array.The following is the syntax −Array.Copy(src, dest, length);Here,src = array to be copieddest = destination arraylength = how many elements to copyThe following is an example showing the usage of Copy(,,) method of array class in C# −Exampleusing System; class Program {    static void Main() {       int[] arrSource = new int[4];       arrSource[0] = 24;       arrSource[1] = 33;       arrSource[2] = 9;       arrSource[3] = 45;       int[] arrTarget = new int[3];       Array.Copy(arrSource, arrTarget, 3);       Console.WriteLine("Destination Array ...");       foreach (int value in arrTarget) {          Console.WriteLine(value);       }    } }

How to convert a 2D array into 1D array in C#?

karthikeya Boyini
Updated on 21-Jun-2020 13:42:39

3K+ Views

Set a two-dimensional array and a one-dimensional array −int[, ] a = new int[2, 2] {{1, 2}, {3, 4} }; int[] b = new int[4];To convert 2D to 1D array, set the two dimensional into one-dimensional we declared before −for (i = 0; i < 2; i++) {    for (j = 0; j < 2; j++) {       b[k++] = a[i, j];    } }The following is the complete code to convert a two-dimensional array to one-dimensional array in C# −Exampleusing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Program {    class twodmatrix { ... Read More

How to convert a Decimal to Octal using C#?

Arjun Thakur
Updated on 21-Jun-2020 13:43:30

402 Views

To get the octal equivalent, use a while loop for the decimal value and store the remainder in the array set for octal. Here we have set the remainder by mod 8 in the array.Then divide the number by 8 −while (dec != 0) {    oct[i] = dec % 8;    dec = dec / 8;    i++; }Let us see the complete code.Here, our decimal number is 18 −using System; namespace Demo {    class Program {       static void Main(string[] args) {          int []oct = new int[30];          // decimal          int dec = 18;          int i = 0;          while (dec != 0){             oct[i] = dec % 8;             dec = dec / 8;             i++;          }          for (int j = i - 1; j >= 0; j--)          Console.Write(oct[j]);          Console.ReadKey();       }    } }

How to compare two lists for equality in C#?

Samual Sam
Updated on 21-Jun-2020 13:44:24

2K+ Views

Set the two lists −List OneList < string > list1 = new List < string > (); list1.Add("A"); list1.Add("B"); list1.Add("C"); list1.Add("D");List TwoList < string > list2 = new List < string > (); list2.Add("C"); list2.Add("D");Now if the following returns different elements, then it would mean the lists are not equal −Exampleusing System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List < string > list1 = new List < string > ();       list1.Add("P");       list1.Add("Q");       list1.Add("R");       Console.WriteLine("First list..."); ... Read More

How to compare two lists and add the difference to a third list in C#?

Ankith Reddy
Updated on 21-Jun-2020 13:47:41

1K+ Views

First, set the two lists −List OneList < string > list1 = new List < string > (); list1.Add("A"); list1.Add("B"); list1.Add("C"); list1.Add("D");List TwoList < string > list2 = new List < string > (); list2.Add("C"); list2.Add("D");To find the difference between the two list and display the difference elements −IEnumerable < string > list3; list3 = list1.Except(list2); foreach(string value in list3) {    Console.WriteLine(value); }The following is the complete example to compare two lists −Exampleusing System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List < string > list1 = new ... Read More

How to compare two dictionaries in C#?

karthikeya Boyini
Updated on 21-Jun-2020 13:18:26

3K+ Views

To compare two dictionaries, firstly set the two dictionaries −Dictionary OneIDictionary d = new Dictionary(); d.Add(1, 97); d.Add(2, 89); d.Add(3, 77); d.Add(4, 88); // Dictionary One elements Console.WriteLine("Dictionary One elements: "+d.Count);Dictionary OneIDictionary d2 = new Dictionary(); d2.Add(1, 97); d2.Add(2, 89); d2.Add(3, 77); d2.Add(4, 88); // Dictionary Two elements Console.WriteLine("Dictionary Two elements: "+d2.Count);Now let us compare them −bool equal = false; if (d.Count == d2.Count) { // Require equal count.    equal = true;    foreach (var pair in d) {       int value;       if (d2.TryGetValue(pair.Key, out value)) {          if ... Read More

How to compare two Dates in C#?

George John
Updated on 21-Jun-2020 13:25:22

1K+ Views

To compare dates in C#, you need to first set two dates to be compared using the DateTime object. We will use the DateTime class in C# −Date 1DateTime date1 = new DateTime(2018, 08, 05); Console.WriteLine("Date 1 : {0}", date1);Date 2DateTime date2 = new DateTime(2018, 08, 07); Console.WriteLine("Date 2 : {0}", date2);Now let us compare both the dates in C#. The following is an example to compare dates in C# −Exampleusing System; namespace Program {    class Demo {       static int Main() {          DateTime date1 = new DateTime(2018, 08, 05);       ... Read More

How to compare two arrays in C#?

Samual Sam
Updated on 21-Jun-2020 13:26:44

1K+ Views

Firstly, set the two arrays to be compared −// two arrays int[] arr = new int[] { 99, 87, 56, 45}; int[] brr = new int[] { 99, 87, 56, 45 };Now, use SequenceEqual() to compare the two arrays −arr.SequenceEqual(brr);The following is the code to compare two arrays −Exampleusing System; using System.Linq; namespace Demo {    class Program {       static void Main(string[] args) {          // two arrays          int[] arr = new int[] { 99, 87, 56, 45};          int[] brr = new int[] { 99, 87, 56, 45 };          // compare          Console.WriteLine(arr.SequenceEqual(brr));       }    } }

How to copy a section of one Array to another in C#?

Chandu yadav
Updated on 21-Jun-2020 13:27:54

3K+ Views

The Array.Copy() method in C# is used to copy section of one array to another array.The following is the syntax −Array.Copy(src, dest, length);Here,src = array to be copieddest = destination arraylength = how many elements to copyThe following is an example showing the usage of Copy(,,) method of array class in C# −Exampleusing System; class Program {    static void Main() {       int[] arrSource = new int[4];       arrSource[0] = 1;       arrSource[1] = 2;       arrSource[2] = 3;       arrSource[3] = 4;       int[] arrTarget = new int[2];       Array.Copy(arrSource, arrTarget, 2);       Console.WriteLine("Destination Array ...");       foreach (int value in arrTarget) {          Console.WriteLine(value);       }    } }

Advertisements