Found 35163 Articles for Programming

How do we pass an array in a method in C#?

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

63 Views

Pass array in a method as a method argument.Let’s say the following is our array declaration and initialization.MyArray app = new MyArray(); /* an int array with 5 elements */ int [] balance = new int[]{1000, 2, 3, 17, 50};Now call the method getAverage() and pass the array as method argument.double getAverage(int[] arr, int size) {    // code }The following is the example showing how to pass an array in a method in C#.Example Live Demousing System; namespace ArrayApplication {    class MyArray {       double getAverage(int[] arr, int size) {          int i;   ... Read More

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

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

3K+ 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

234 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 enum keyword to define a variable type in C#?

karthikeya Boyini
Updated on 23-Jun-2020 14:29:11

62 Views

C# enumerations are value data type. An enumeration is a set of named integer constants. An enumerated type is declared using the enum keyword.The following is the syntax of enum.enum {    enumeration list };Let us see an example.enum Vehicle { Car, Bus, Truck };The following is an example showing how to use enum keyword to define variable type.Example Live Demousing System; namespace Demo {    class Program {       enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };       static void Main(string[] args) {          int WeekdayStart = (int)Days.Mon;   ... Read More

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

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

137 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

93 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 we pass parameters by reference in a C# method?

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

109 Views

A reference parameter is a reference to a memory location of a variable. When you pass parameters by reference, unlike value parameters, a new storage location is not created for these parameters.The reference parameters represent the same memory location as the actual parameters that are supplied to the method.The following is an example displaying how to pass parameters by reference. Declare the reference parameters using the ref keyword.Example Live Demousing System; namespace CalculatorApplication {    class NumberManipulator {       public void swap(ref int x, ref int y) {          int temp;          temp ... Read More

How do we use nested switch statements in C#?

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

110 Views

In C#, you can use one switch statement inside another switch statement. It is possible to have a switch as part of the statement sequence of an outer switch. Even if the case constants of the inner and outer switch contain common values, no conflicts will arise.The following is the syntax.switch(ch1) {    case 'A':    Console.WriteLine("This A is part of outer switch" );    switch(ch2) {       case 'A':       Console.WriteLine("This A is part of inner switch" );       break;       case 'B': /* inner B case code */    } ... 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

145 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 create a structure in C#?

karthikeya Boyini
Updated on 23-Jun-2020 14:21:00

47 Views

In C#, a structure is a value type data type. It helps you to make a single variable hold related data of various data types. The struct keyword is used for creating a structure.To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member for your program.For example, here is the way you can declare the Book structure.struct Books {    public string title;    public string author;    public string subject;    public int book_id; };The following is an example showing how to create structure in C#.Example Live ... Read More

Advertisements