Found 34494 Articles for Programming

Dimensional Array in C#?

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

151 Views

C# allows multidimensional arrays. Declare a 2-dimensional array of int as.int [ , , ] a;The simplest form of the multidimensional array is the 2-dimensional array. A 2-dimensional array is a list of one-dimensional arrays.The following is a two-dimensional array with 3 rows and 4 columns.Let us now see an example to work with multi-dimensional arrays in C#.Example Live Demousing System; namespace ArrayApplication {    class MyArray {       static void Main(string[] args) {          /* an array with 5 rows and 2 columns*/          int[, ] a = new int[5, 2] {{0, ... Read More

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

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

353 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

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

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

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

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

305 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

113 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

C# program to determine if a string has all unique characters

George John
Updated on 23-Jun-2020 11:34:36

2K+ Views

Use the substring() method in C# to check each and every substring for unique characters. Loop it until the length of the string.If any one the substring matches another, then it would mean that the string do not have unique characters.You can try to run the following code to determine if a string has all unique characters.Example Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; public class Demo {    public bool CheckUnique(string str) {       string one = "";       string two = "";       for (int i = 0; i ... Read More

Advertisements