Found 2628 Articles for Csharp

How to convert a Decimal to Octal using C#?

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

299 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

889 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

993 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);       }    } }

How are parameters passed in C#?

karthikeya Boyini
Updated on 21-Jun-2020 13:29:06

97 Views

Parameters are passed in C# either by value or by reference. With that, you can also use out parameters and param array to pass parameters −ValueThis method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.ReferenceThis method copies the reference to the memory location of an argument into the formal parameter. This means that changes made to the parameter affect the argument.OutA return statement can be used for returning only one value from a function. However, using output ... Read More

How command line arguments are passed in main method in C#?

Arjun Thakur
Updated on 21-Jun-2020 13:31:42

269 Views

The Main() method is the entry point −static void Main(string[] args)The arguments array args is used to set arguments −string[] args)It will set the following if you add two arguments −var args = new string[] {"arg1", "arg2”}Here is the demo code −Exampleusing System; namespace Demo {    class HelloWorld {       // args for command line       static void Main(string[] args) {          Console.WriteLine("Welcome here!");          Console.ReadKey();       }    } }To compile a C# program by using the command-line instead of the Visual Studio IDE ... Read More

How to access elements from an array in C#?

Ankith Reddy
Updated on 21-Jun-2020 13:09:21

733 Views

First, define and initalize an array −int[] p = new int[3] {99, 92, 95};Now, display the array elements −for (j = 0; j < 3; j++ ) {    Console.WriteLine("Price of Product[{0}] = {1}", j, p[j]); }To acess any element, just include the index of the element you want like this −p[2];The above is to acess the 3rd element.Now let us see the complete code −Exampleusing System; namespace Program {    class Demo {       static void Main(string[] args) {          int[] p = new int[3] {99, 92, 95};          int j;          for (j = 0; j < 3; j++ ) {             Console.WriteLine("Price of Product[{0}] = {1}", j, p[j]);          }          // access          int e = p[2];          Console.WriteLine("Product 3rd price: "+e);          Console.ReadKey();       }    } }

Advertisements