Csharp Articles - Page 124 of 258

How do we use a #line directive in C#?

Samual Sam
Updated on 30-Jul-2019 22:30:23

234 Views

It lets you modify the compiler's line number and (optionally) the file name output for errors and warnings. Let us see some examples. #line 100 "demo" int a; // CS0168 on line 100 int b; // CS0168 on line 101 int c; // CS0168 on line 102 As shown above the example reports three warnings associated with line numbers. The #line 100 directive forces the line number to be 100 and until the next #line directive, the filename will be reported as "demo”. Let’s see ... Read More

How do you use ‘foreach’ statement for accessing array elements in C#

karthikeya Boyini
Updated on 23-Jun-2020 14:39:01

172 Views

To access Array elements in a foreach statement, use the numeric index.Let’s say the following is our code.Example Live Demousing System; namespace ArrayApplication {    class MyArray {       static void Main(string[] args) {          int [] n = new int[10]; /* n is an array of 10 integers */          /* initialize elements of array n */          for ( int i = 0; i < 10; i++ ) {             n[i] = i + 100;          }       ... Read More

How do I sort a two-dimensional array in C#

karthikeya Boyini
Updated on 23-Jun-2020 14:41:25

2K+ Views

To sort a two-dimensional array in C#, in a nested for loop, add another for loop to check the following condition.Examplefor (int k = 0; k < j; k++) {    if (arr[i, k] > arr[i, k + 1]) {       int myTemp = arr[i, k];       arr[i, k] = arr[i, k + 1];       arr[i, k + 1] = myTemp;    } }Till the outer loop loops through, use the GetLength() method as shown below. This is done to sort the array.Examplefor (int i = 0; i < arr.GetLength(0); i++) {    for ... Read More

How do we use multi-dimensional arrays in C#?

karthikeya Boyini
Updated on 23-Jun-2020 14:45:08

253 Views

C# allows multidimensional arrays. Multi-dimensional arrays are also called rectangular array. Declare a 2-dimensional array of strings as.string [, ] names;A 2-dimensional array can be thought of as a table, which has x number of rows and y number of columns.Multidimensional arrays may be initialized by specifying bracketed values for each row. The following array is with 4 rows and each row has 4 columns.int [, ] a = new int [4, 4] {    {0, 1, 2, 3} , /* initializers for row indexed by 0 */    {4, 5, 6, 7} , /* initializers for row indexed by ... Read More

How do you sort an array in C# in descending order?

karthikeya Boyini
Updated on 23-Jun-2020 14:46:40

5K+ 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 an 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 do we use foreach statement to loop through the elements of an array in C#?

Samual Sam
Updated on 23-Jun-2020 14:28:39

376 Views

A foreach loop is used to execute a statement or a group of statements for each element in an array or collection.It is similar to for Loop; however, the loop is executed for each element in an array or group. Therefoe, the index does not exist in it.Let us see an example of Bubble Sort, wherein after sorting the elements, we will display the elements using the foreach loop.Example Live Demousing System; namespace BubbleSort {    class MySort {       static void Main(string[] args) {          int[] arr = { 78, 55, 45, 98, 13 };          int temp;          for (int j = 0; j

How do we use continue statement in a while loop in C#?

Samual Sam
Updated on 23-Jun-2020 14:31:50

277 Views

The continue statement causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.The continue statement in C# works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between.For the while loop, continue statement causes the program control passes to the conditional tests.The following is the complete code to use continue statement in a while loop.Example Live Demousing System; namespace Demo {    class Program {       static void Main(string[] args) {         ... Read More

How do we pass parameters by value in a C# method?

karthikeya Boyini
Updated on 23-Jun-2020 14:32:38

175 Views

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. The following is the code to pass parameters by value.Example Live Demousing System; namespace CalculatorApplication {    class NumberManipulator {       public void swap(int x, int y) {          int temp;          temp = x; /* save the value ... Read More

How do you use a ‘for loop’ for accessing array elements in C#?

Samual Sam
Updated on 23-Jun-2020 14:35:45

326 Views

The ‘for loop’ executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.The following is our for loop.Example Live Demousing System; namespace ArrayApplication {    class MyArray {       static void Main(string[] args) {          int [] n = new int[10]; /* n is an array of 10 integers */          int i, j;          /* initialize elements of array n */          for ( i = 0; i < 10; i++ ) {             n[ ... Read More

How do we call a C# method recursively?

Samual Sam
Updated on 23-Jun-2020 14:21:46

330 Views

To call a C# method recursively, you can try to run the following code. Here, Factorial of a number is what we are finding using a recursive function display().If the value is 1, it returns 1 since Factorial is 1.if (n == 1) return 1;If not, then the recursive function will be called for the following iterations if 1you want the value of 5!Interation1: 5 * display(5 - 1); Interation2: 4 * display(4 - 1); Interation3: 3 * display(3 - 1); Interation4: 4 * display(2 - 1);The following is the complete code to call a C# method recursively.Example Live Demousing System; ... Read More

Advertisements