Found 35163 Articles for Programming

How to use the CopyTo(,) method of array class in C#

Samual Sam
Updated on 23-Jun-2020 12:34:39

2K+ Views

The CopyTo() method in C# is used to copy elements of one array to another array. In this method, you can set the starting index from where you want to copy from the source array.The following is the syntax.CopyTo(dest, index);Here dest = destination arrayindex= starting indexThe following is an example showing the usage of CopyTo(, ) method of array class in C#.Example Live Demousing System; class Program {    static void Main() {       int[] arrSource = new int[4];       arrSource[0] = 5;       arrSource[1] = 9;       arrSource[2] = 1;     ... Read More

How to use the Copy(, ,) method of array class in C#

Arjun Thakur
Updated on 23-Jun-2020 12:35:11

262 Views

As the name suggests the Array.Copy() method in C# is used to copy elements of one array to another array.The following is the syntax.Array.Copy(src, dest, length);Heresrc = 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#.Example Live Demousing System; class Program {    static void Main() {       int[] arrSource = new int[4];       arrSource[0] = 99;       arrSource[1] = 66;       arrSource[2] = 111;       arrSource[3] = 33;       int[] arrTarget = new int[4];       Array.Copy(arrSource, arrTarget, 4);       Console.WriteLine("Destination Array ...");       foreach (int value in arrTarget) {          Console.WriteLine(value);       }    } }OutputDestination Array ... 99 66 111 33

How to use the Clone() method of array class in C#?

karthikeya Boyini
Updated on 23-Jun-2020 12:36:09

3K+ Views

The Clone() method in C# is used to clone the existing array.Firstly, set the array to be cloned.string[] arr = { "Web", "World"};Now clone the array created above using the array.Clone() method.string[] arrClone = array.Clone() as string[];Let us see the complete example.Exampleusing System; class Program {    static void Main() {       string[] arr = { "Web", "World"};       Console.WriteLine(string.Join(",", arr));       string[] arrClone = array.Clone() as string[];       Console.WriteLine(string.Join(",", arrClone));       Console.WriteLine();    } }

How to use the Clear method of array class in C#?

Chandu yadav
Updated on 23-Jun-2020 12:36:43

220 Views

The Array.Clear class in C# clears i.e.zeros out all elements.In the below example, we have first considered an array with 3 elements.int[] arr = new int[] { 11, 40, 20};Now we have used the Array.clear method to zero out all the arrays.Array.Clear(arr, 0, arr.Length);Let us see an example of Array.clear method in c#.Example Live Demousing System; class Program {    static void Main() {       int[] arr = new int[] {11, 40, 20};       Console.WriteLine("Array (Old):");       foreach (int val in arr) {          Console.WriteLine(val);       }       ... Read More

How to use the ?: conditional operator in C#?

Samual Sam
Updated on 23-Jun-2020 12:37:42

233 Views

A conditional operator is represented by the symbol '?:' The first operand is the evaluating expression. It has right to left associativity.The syntax for conditional operator.expression ? expression : expressionThe conditional operator works as follows −The first operand is implicitly converted to bool.If the first operand evaluates to true, the second operand is evaluated.If the first operand evaluates to false, the third operand is evaluated.Remember, only one of the last two operands is evaluated in a conditional expression.Example Live Demousing System; namespace Demo {    class Program {       static void Main(string[] args) {          int ... Read More

How to sort an array in C#?

George John
Updated on 23-Jun-2020 12:38:10

1K+ Views

The following is the integer array.int[] arr = { 99, 43, 86 };To sort, use the Sort() method.Array.Sort(arr);The following is the complete code displaying how to sort an array in C# using the Sort() method.Example Live Demousing System; class Demo {    static void Main() {       int[] arr = { 99, 43, 86 };       // sort       Array.Sort(arr);       Console.WriteLine("Sorted List");       foreach (int res in arr) {          Console.Write(""+res);       }       Console.WriteLine();    } }OutputSorted List 43 86 99

How to sort one dimensional array in descending order using Array Class method?

karthikeya Boyini
Updated on 23-Jun-2020 12:38:54

191 Views

The following is the unsorted array.int[] list = {98, 23, 97, 36, 77};Now first use the Sort() method to sort the array.Array.Reverse(list);Use the Reverse() method that would eventually give you a sorted array in descending order.Array.Reverse(list);You can try to run the following code to to sort one dimensional array in descending order.Example Live Demousing System; namespace Demo {    public class MyApplication {       public static void Main(string[] args) {          int[] list = {98, 23, 97, 36, 77};          Console.WriteLine("Original Unsorted List");          foreach (int i in list) { ... Read More

How to use StringBuilder in C#?

Ankith Reddy
Updated on 23-Jun-2020 12:39:48

236 Views

With StringBuilder, you can expand the number of characters in the string. String cannot be changed once it is created, but StringBuildercan be expanded. It does not create a new object in the memory.Initialize StringBuilder.StringBuilder str = new StringBuilder();Let us see an example to learn how to work with StringBuilder in C#.Example Live Demousing System; using System.Text; public class Program {    public static void Main() {       StringBuilder str = new StringBuilder("Web World!!",30);       str.Replace("World", "Arena");       Console.WriteLine(str);    } }OutputWeb Arena!!Above the Replace() method of StringBuilder is used to to replace a string in C#.

What are C# pre-processor directives?

Samual Sam
Updated on 23-Jun-2020 12:27:07

302 Views

The preprocessor directives give instruction to the compiler to preprocess the information before actual compilation starts.All preprocessor directives begin with #, and only white-space characters may appear before a preprocessor directive on a line. Preprocessor directives are not statements, so they do not end with a semicolon (;).The following are some of the preprocessor directives in C#.Sr.NoPreprocessor Directive & Description1#defineIt defines a sequence of characters, called symbol.2#undefIt allows you to undefine a symbol.3#ifIt allows testing a symbol or symbols to see if they evaluate to true.4#elseIt allows to create a compound conditional directive, along with #if.5#elifIt allows creating a compound ... Read More

What are Booleans types in C#?

Arjun Thakur
Updated on 23-Jun-2020 12:28:29

99 Views

For the Boolean type, the bool keyword is used and is an alias of System.Boolean.It is used to declare variables to store the Boolean values, true and false.Let us see an example to learn how to use bool in C#.Exampleusing System; public class Demo {    static void Main() {       bool val = true;       int d = DateTime.Now.DayOfYear;       val = (d % 2 == 0);       if (val) {          Console.WriteLine("days: even number");       } else {          Console.WriteLine("days:odd number");       }    } }

Advertisements