Found 33676 Articles for Programming

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

How are parameters passed in C#?

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

200 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

347 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

894 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();       }    } }

How to access elements from a rectangular array in C#?

karthikeya Boyini
Updated on 21-Jun-2020 13:11:30

291 Views

To access elements from a rectangular array, you just need to set the index of which you want to get the element. Multi-dimensional arrays are also called rectangular array −a[0, 1]; // second elementThe following is an example showing how to work with a rectangular array in C# and access an element −Exampleusing System; namespace Demo {    class Program {       static void Main(string[] args) {          int[, ] a = new int[3, 3];          a[0, 0]= 10;          a[0, 1]= 20;       ... Read More

How to convert Upper case to Lower Case using C#?

George John
Updated on 21-Jun-2020 13:10:21

3K+ Views

To convert Upper case to Lower case, use the ToLower() method in C#.Let’s say your string is −str = "TIM";To convert the above uppercase string in lowercase, use the ToLower() method −Console.WriteLine("Converted to LowerCase : {0}", str.ToLower());The following is the code in C# to convert character case −Exampleusing System; using System.Collections.Generic; using System.Text; namespace Demo {    class MyApplication {       static void Main(string[] args) {          string str;          str = "TIM";          Console.WriteLine("UpperCase : {0}", str);          // convert to lowercase          Console.WriteLine("Converted to LowerCase : {0}", str.ToLower());          Console.ReadLine();       }    } }

How to convert Trigonometric Angles in Radians using C#?

Samual Sam
Updated on 21-Jun-2020 13:13:04

3K+ Views

To convert trigonometric angles in Radians, multiply by Math.PI/180. This will convert degrees to radians.The following is the code −Exampleusing System; class Program {    static void Main() {       Console.WriteLine(Math.Cos(45));       double res = Math.Cos(Math.PI * 45 / 180.0);       Console.WriteLine(res);    } }Above, we converted using the following formulae −Math.PI * angle / 180.0

Value parameters vs Reference parameters vs Output Parameters in C#

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

3K+ Views

Value parametersThe value parameters copy 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.This is the default mechanism for passing parameters to a method. In this mechanism, when a method is called, a new storage location is created for each value parameter.The values of the actual parameters are copied into them. Hence, the changes made to the parameter inside the method have no effect on the argument.Reference ParametersA reference parameter is a reference to a memory location of a ... Read More

How to convert Lower case to Upper Case using C#?

karthikeya Boyini
Updated on 21-Jun-2020 12:53:47

2K+ Views

To convert Lower case to Upper case, use the ToUpper() method in C#.Let’s say your string is −str = "david";To convert the above lowercase string in uppercase, use the ToUpper() method −Console.WriteLine("Converted to UpperCase : {0}", str.ToUpper());The following is the code in C# to convert character case −Exampleusing System; using System.Collections.Generic; using System.Text; namespace Demo {    class MyApplication {       static void Main(string[] args) {          string str;          str = "david";          Console.WriteLine("LowerCase : {0}", str);          // convert to uppercase          Console.WriteLine("Converted to UpperCase : {0}", str.ToUpper());          Console.ReadLine();       }    } }

Advertisements