Found 27758 Articles for Server Side Programming

How do you use ‘foreach’ loop for iterating over an array in C#?

karthikeya Boyini
Updated on 23-Jun-2020 11:41:04

286 Views

The for each loop similar to the for loop; however, the loop is executed for each element in an array or group. Therefore, the index does not exist in foreach loop.Let us see an example of Bubble Sort, wherein after sorting the elements, we will display the elements using the foreach loop.foreach (int p in arr) Console.Write(p + " ");The following is the complete example.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

Difference between Method and Function in C#

George John
Updated on 23-Jun-2020 11:40:18

5K+ Views

Methods and Functions are the same in C#.However, Methods are used in C# and are functions that operate through a designated class. A method is a group of statements that together perform a task. Every C# program has at least one class with a method named Main.The following is a simple example showing how to create methods in C#.Exampleclass NumberManipulator {    public int FindMax(int num1, int num2) {       /* local variable declaration */       int result;       if (num1 > num2) {          result = num1;       }else {          result = num2;       }       return result;    }    ... }

Compressing and Decompressing files using GZIP Format in C#

Samual Sam
Updated on 23-Jun-2020 11:42:42

2K+ Views

To compress and decompress files using GZIP Format, use the GZipStream class.CompressTo zip a file, use the GZipStream class with the FileStream class. Set the following parameters.File to be zipped and the name of the output zip file.Here, outputFile is the output file and the file is read into the FileStream.Exampleusing(var compress = new GZipStream(outputFile, CompressionMode.Compress, false)) {    byte[] b = new byte[inFile.Length];    int read = inFile.Read(b, 0, b.Length);    while (read > 0) {       compress.Write(b, 0, read);       read = inFile.Read(b, 0, b.Length);    } }DecompressTo decompress a file, use the same ... Read More

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

Ankith Reddy
Updated on 23-Jun-2020 11:43:34

8K+ Views

Firstly, set the unsorted array.int[] list = {98, 23, 97, 36, 77};Sort the array using the Sort() method.Array.Sort(list);You can try to run the following code to to sort an array in ascending 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) {             Console.Write(i + " ");          }          Array.Sort(list);          Console.WriteLine("Sorted List");          for(int i=0; i

How to initialize two-dimensional arrays in C#?

Arjun Thakur
Updated on 23-Jun-2020 11:28:14

15K+ Views

A 2-dimensional array is a list of one-dimensional arrays.Two-dimensional arrays may be initialized by specifying bracketed values for each row.int [,] a = new int [4,4] {    {0, 1, 2, 3} ,    {4, 5, 6, 7} ,    {8, 9, 10, 11} ,    {12, 13, 14, 15} };The following is an example showing how to work with two-dimensional arrays in C#.Example Live Demousing System; namespace ArrayApplication {    class MyArray {       static void Main(string[] args) {          /* an array with 3 rows and 2 columns*/          int[,] a = new int[3, 2] {{0,0}, {1,2}, {2,4} };          int i, j;          /* output each array element's value */          for (i = 0; i < 3; i++) {             for (j = 0; j < 2; j++) {                Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i,j]);             }          }          Console.ReadKey();       }    } }Outputa[0,0] = 0 a[0,1] = 0 a[1,0] = 1 a[1,1] = 2 a[2,0] = 2 a[2,1] = 4

How to initialize multi-dimensional arrays in C#?

karthikeya Boyini
Updated on 23-Jun-2020 11:27:22

413 Views

The simplest form of the multidimensional array is the 2-dimensional array. A 2-dimensional array is a list of one-dimensional arrays.Multidimensional arrays may be initialized by specifying bracketed values for each row.int [, ] a = new int [3, 4] {    {0, 1, 2, 3} , /* initializers for row indexed by 0 */    {4, 5, 6, 7} , /* initializers for row indexed by 1 */    {8, 9, 10, 11} /* initializers for row indexed by 2 */ };The following is an example showing how to work with multi-dimensional arrays in C#.Example Live Demousing System; namespace ArrayApplication { ... Read More

What is the Queue class in C#?

Samual Sam
Updated on 23-Jun-2020 11:30:21

105 Views

To represent a first-in first-out collection of objects in C#, use the Queue class. When you add an item in the list, it is called enqueue, and when you remove an item, it is called deque.Some of the methods of the Queue class include.Sr.NoMethod & Description1public virtual void Clear();Removes all elements from the Queue.2public virtual bool Contains(object obj);Determines whether an element is in the Queue.3public virtual object Dequeue();Removes and returns the object at the beginning of the Queue.4public virtual void Enqueue(object obj);Adds an object to the end of the Queue.5public virtual object[] ToArray();Copies the Queue to a new array.Let us ... Read More

What is the scope of a private member variable of a class in C#?

Chandu yadav
Updated on 23-Jun-2020 11:29:11

284 Views

Only functions of the same class can access its private members. Private access specifier allows a class to hide its member variables and member functions from other functions and objects.Example Live Demousing System; namespace RectangleApplication {    class Rectangle {       //member variables       private double length;       private double width;       public void Acceptdetails() {          length = 10;          width = 14;       }       public double GetArea() {          return length * width;       } ... Read More

What is the purpose of ‘as’ operator in C#?

Chandu yadav
Updated on 23-Jun-2020 11:32:35

97 Views

The "as" operator perform conversions between compatible types. It is like a cast operation and it performs only reference conversions, nullable conversions, and boxing conversions. The as operator can't perform other conversions, such as user-defined conversions, which should instead be performed by using cast expressions.The following is an example showing the usage of as operation in C#. Here as is used for conversion.string s = obj[i] as string;Try to run the following code to work with ‘as’ operator in C#.Example Live Demousing System; public class Demo {    public static void Main() {       object[] obj = new object[2]; ... Read More

C# program to sort an array in descending order

Samual Sam
Updated on 23-Jun-2020 11:33:39

3K+ Views

Initialize the array.int[] myArr = new int[5] {98, 76, 99, 32, 77};Compare the first element in the array with the next element to find the largest element, then the second largest, etc.if(myArr[i] < myArr[j]) {    temp = myArr[i];    myArr[i] = myArr[j];    myArr[j] = temp; }Above, i and j are initially set to.i=0; j=i+1;Try to run the following code to sort an array in descending order.Example Live Demousing System; public class Demo {    public static void Main() {       int[] myArr = new int[5] {98, 76, 99, 32, 77};       int i, j, temp;       Console.Write("Elements: ");       for(i=0;i

Advertisements