Csharp Articles

Page 158 of 196

What are static or fixed length arrays in C#?

Samual Sam
Samual Sam
Updated on 21-Jun-2020 2K+ Views

A static array is a data structure with a fixed size. Let us see an example of a static array in C#.Here is a static string array. The data remains the same here i.e. fixed −static string[] _fruits = new string[] {    "apple",    "mango" };Now let us see the complete example to create and access static arrays in C# −Exampleusing System; class Demo {    static void Main() {       foreach (string fruits in Program.Fruits) {          Console.WriteLine(fruits);       }    } } public static class Program {    static string[] _fruits = new string[] {       "apple",       "mango"    };    public static string[] Fruits {       get {          return _fruits;       }    } }

Read More

What is a dictionary in C#?

Samual Sam
Samual Sam
Updated on 20-Jun-2020 3K+ Views

Dictionary is a collection of keys and values in C#. Dictionary is included in the System.Collection.Generics namespace.To declare and initialize a Dictionary −IDictionary d = new Dictionary();Above, types of key and value are set while declaring a dictionary object. An int is a type of key and string is a type of value. Both will get stored in a dictionary object named d.Let us now see an example −Exampleusing System; using System.Collections.Generic; public class Demo {    public static void Main() {       IDictionary d = new Dictionary();       d.Add(1, 97);       d.Add(2, ...

Read More

What is the best way to iterate over a Dictionary in C#?

Samual Sam
Samual Sam
Updated on 20-Jun-2020 2K+ Views

In a Dictionary collection, store any data type. Dictionary is a collection of keys and values in C#. Dictionary is included in the System.Collection.Generics namespace.Let us now see the best way to iterate over a Dictionary in C# −Firstly, let us create a dictionary −var d = new Dictionary(5);Now add the key and value −// add key and value d.Add("car", 25); d.Add("bus", 28); d.Add("motorbike", 17);Use orderby to order by values −var val = from ele in d orderby ele.Value ascending select ele;We have set ascending above to sort the dictionary in ascending order. You can also use descending.Display the values ...

Read More

Thread Pools in C#

karthikeya Boyini
karthikeya Boyini
Updated on 20-Jun-2020 4K+ Views

Thread pool in C# is a collection of threads. It is used to perform tasks in the background. When a thread completes a task, it is sent to the queue wherein all the waiting threads are present. This is done so that it can be reused.Let us see how to create a thread pool.Firstly, use the following namespace −using System.Threading;Now, call the threadpool class, using the threadpool object. Call the method QueueUserWorkItem −ThreadPool.QueueUserWorkItem(new WaitCallback(Run)); Iterate this in a loop and compare it with a normal Thread object.

Read More

What are multicasting delegates in C#?

Samual Sam
Samual Sam
Updated on 20-Jun-2020 2K+ Views

A delegate that holds a reference to more than one method is called multicasting delegate.Let us see an example −Exampleusing System; delegate void myDelegate(int val1, int val2); public class Demo {    public static void CalAdd(int val1, int val2) {       Console.WriteLine("{0} + {1} = {2}", val1, val2, val1 + val2);    }    public static void CalSub(int val1, int val2) {       Console.WriteLine("{0} - {1} = {2}", val1, val2, val1 - val2);    } } public class Program {    static void Main() {       myDelegate d = new myDelegate(Demo.CalAdd);   ...

Read More

What is the base class for all exceptions in C#?

Arjun Thakur
Arjun Thakur
Updated on 20-Jun-2020 2K+ Views

The System.SystemException class is the base class for all predefined system exception. Some of the exception classes derived from the System.Exception class are the System.ApplicationException and System.SystemException classes.The System.ApplicationException class supports exceptions generated by application programs. Hence the exceptions defined by the programmers should derive from this class.The following are the exceptions under the base class System.SystemException −Sr.No.Exception Class & Description1System.IO.IOExceptionHandles I/O errors.2System.IndexOutOfRangeExceptionHandles errors generated when a method refers to an array index out of range.3System.ArrayTypeMismatchExceptionHandles errors generated when type is mismatched with the array type.4System.NullReferenceExceptionHandles errors generated from referencing a null object.5System.DivideByZeroExceptionHandles errors generated from dividing a dividend with zero.6System.InvalidCastExceptionHandles errors ...

Read More

Try-Catch-Finally in C#

Samual Sam
Samual Sam
Updated on 20-Jun-2020 7K+ Views

C# exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.C# exception handling is performed using the following keywords −try − A try block identifies a block of code for which particular exceptions is activated. It is followed by one or more catch blocks.catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.finally − The finally block is used to execute a given set ...

Read More

volatile keyword in C#

Samual Sam
Samual Sam
Updated on 20-Jun-2020 821 Views

To reduce concurrency issues in C#, use the volatile keyword. Let us seen an example.The following is how you use a volatile keyword for public variable −class Program {    public volatile int a;    public void Program(int _a) {       i = _i;    } }Let us see another example: We have two static variables. Set them in a new method −_out = "Welcome!"; _new = true;We declared them as static before using volatile −static string _out; static volatile bool new;Now you need to run the method on a thread −new Thread(new ThreadStart(volatileFunc)).Start();Read the value of the ...

Read More

Try/catch/finally/throw keywords in C#

karthikeya Boyini
karthikeya Boyini
Updated on 20-Jun-2020 2K+ Views

Exception handling is based on the following keywords and its usage −try − A try block identifies a block of code for which particular exceptions is activated. It is followed by one or more catch blocks.catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.finally − The finally block is used to execute a given set of statements, whether an exception is thrown or not thrown. For example, if you open a file, it must be closed ...

Read More

What is the base class for all data types in C#.NET?

karthikeya Boyini
karthikeya Boyini
Updated on 20-Jun-2020 2K+ Views

Object is the base class for all data types in C#. The Object Type is the ultimate base class for all data types in C# Common Type System (CTS). The object is an alias for System.Object class.When a value type is converted to object type, it is called boxing and on the other hand, when an object type is converted to a value type, it is called unboxing.The following is an example showing the usage of object data types −using System; using System.IO; namespace Demo {    class objectClass {       public int x = 56;   ...

Read More
Showing 1571–1580 of 1,951 articles
« Prev 1 156 157 158 159 160 196 Next »
Advertisements