Found 2587 Articles for Csharp

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

Ankith Reddy
Updated on 23-Jun-2020 11:36:24

474 Views

Protected access specifier allows a child class to access the member variables and member functions of its base class. This way it helps in implementing inheritance. We will discuss this in more details in the inheritance chapter.The following is an example showing we have set an protected member variable in Class A.class A {    protected int a2 = 87; }Now under the derived class when we will try to access the above variable from the derived class object, then it would work fine as shown below −Exampleusing System; class A {    protected int a2 = 87; } class ... Read More

Difference between C# and Visual C#

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

2K+ Views

C# and Visual C# are both the same. When you use Visual Studio for C# development, it is called Visual C# .Consider Visual C# as an implementation of C#. Microsoft Visual Studio is an IDE from Microsoft to develop programs, web app, web services, etc. The current version of Visual Studio is Visual Studio 2017, that supports .NET 3.5 to 4.7 framework. C# is a multi-paradigm programming language whose current version is C# 7.3. The following reasons make C# a widely used professional language − It is a modern, general-purpose programming language It is object oriented. It is ... Read More

How to find minimum between 2 numbers using C#?

Samual Sam
Updated on 23-Jun-2020 11:37:27

2K+ Views

Firstly, declare and initialize two numbers.int num1 = 35; int num2 = 55;With that, use if- else to find the minimum number.if (num1 < num2) {    minNum = num1; } else {    minNum = num2; }Above, we have set the minimum value to the variable minNum and printed it later on.The following is the complete example to find minimum between 2 numbers in C#.Example Live Demousing System; namespace Demo {    class Program {       static void Main(string[] args) {          int num1 = 50;          int num2 = 90;   ... Read More

What is the Item property of SortedList class in C#?

karthikeya Boyini
Updated on 23-Jun-2020 11:38:52

93 Views

A sorted list is a combination of an array and a hash table. It contains a list of items that can be accessed using a key or an index.Gets and sets the value associated with a specific key in the SortedList.You can also use the Item property to add new elements.If a key does not exist, then you can include it like.myCollection["myNonexistentKey"] = myValueIf the key already exist, then it will overwrite it with the new key and value.The following is an example to learn how to work with Item property of SorteList class in C#.Example Live Demousing System; using System.Collections; ... Read More

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

Chandu yadav
Updated on 23-Jun-2020 11:38:17

337 Views

The protected internal access specifier allows a class to hide its member variables and member functions from other class objects and functions, except a child class within the same application.In the below example, the derived class object can access the protected internal variable.Example Live Demousing System; class One {    protected internal int a = 50;    private int b; } class Two : One {    public Two() {       Console.WriteLine(this.a);    } } class Demo {    static void Main() {       Two t = new Two();       // allowed since it is a derived class object       t.a = 20;    } }Output50

Dimensional Array in C#?

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

224 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

477 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

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

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

Advertisements