What are Delegates in C#

Samual Sam
Updated on 20-Jun-2020 10:21:40

370 Views

A delegate in C# is a reference to the method. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime.Delegates are especially used for implementing events and the call-back methods. All delegates are implicitly derived from the System.Delegate class.Let us see how to declare delegates in C#.delegate Let us see an example to learn how to work with Delegates in C#.Example Live Demousing System; using System.IO; namespace DelegateAppl {    class PrintString {       static FileStream fs;       static StreamWriter sw; ... Read More

What are the Custom Exceptions in C#

karthikeya Boyini
Updated on 20-Jun-2020 10:20:05

855 Views

Like any other programming language, in C#, you can easily create a user-defined exception. User-defined exception classes are derived from the Exception class. Custom exceptions are what we call user-defined exceptions.In the below example, the exception created is not a built-in exception; it is a custom exception −TempIsZeroExceptionYou can try to run the following code to learn how to create a user-defined exception in C#.Example Live Demousing System; namespace Demo {    class TestTemperature {       static void Main(string[] args) {          Temperature temp = new Temperature();          try {     ... Read More

What are Destructors in C# Programs

karthikeya Boyini
Updated on 20-Jun-2020 10:17:43

2K+ Views

A destructor is a special member function of a class that is executed whenever an object of its class goes out of scope.It has exactly the same name as that of the class with a prefixed tilde (~), for example, our class name is Demo.public Demo() { // constructor    Console.WriteLine("Object is being created"); } ~Demo() { //destructor    Console.WriteLine("Object is being deleted"); }Let us see an example to learn how to work with Destructor in C#.Example Live Demousing System; namespace LineApplication {    class Line {       private double length; // Length of a line   ... Read More

Different Methods of Passing Parameters in C#

Samual Sam
Updated on 20-Jun-2020 10:16:10

433 Views

When a method with parameters is called, you need to pass the parameters to the method using any of the following three methods -Reference ParametersThis method copies the reference to the memory location of an argument into the formal parameter. This means that changes made to the parameter affect the argument.Value ParametersThis method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.In Value parameters, when a method is called, a new storage location is created for each value ... Read More

Declare an Empty String Array in C#

Samual Sam
Updated on 20-Jun-2020 10:12:28

3K+ Views

In C#, you can use strings as an array of characters, However, more common practice is to use the string keyword to declare a string variable. The string keyword is an alias for the System.String class.To declare an empty string.string[] arr = new string[] {}; // empty stringNow let us see what will happen when we will print this empty string.Example Live Demousing System; namespace Demo {    class Program {       static void Main(string[] args) {          string[] arr = new string[] {}; // empty string         ... Read More

Check If a Number Is Prime Using C#

Samual Sam
Updated on 20-Jun-2020 10:09:09

4K+ Views

To calculate whether a number is prime or not, we have used a loop and within that on every iteration, we have an if statement to find that the remainder is equal to 0, between the number itself.for (int i = 1; i

Use the Sleep Method in C#

karthikeya Boyini
Updated on 20-Jun-2020 10:08:15

840 Views

The sleep method of the thread is used to pause the thread for a specific period.If you want to set sleep for some seconds, then use it like the following code snippet −int sleepfor = 2000; Thread.Sleep(sleepfor);You can try to run the following code to implement the sleep method of the thread −Example Live Demousing System; using System.Threading; namespace MyApplication {    class ThreadCreationProgram {       public static void CallToChildThread() {          Console.WriteLine("Child thread starts");          int sleepfor = 2000;          Console.WriteLine("Child Thread Paused for {0} seconds", ... Read More

Use Right Shift Operators in C#

Samual Sam
Updated on 20-Jun-2020 10:07:26

179 Views

The left operands value is moved right by the number of bits specified by the right operand in Right Shift Operator.Let us see an example of Right Shift operator in C# −using System; namespace OperatorsAppl {    class Program {       static void Main(string[] args) {          int a = 60; /* 60 = 0011 1100 */          int b = 0;          b = a >> 2; /* 15 = 0000 1111 */          Console.WriteLine("Right Shift Operator - Value of b is {0}", b);          Console.ReadLine();       }    } }Above, the value of a is 60 i.e. 0011 1100 in binary.Set right shift operator as shown in the above example. This shifts the bits to the right twice −a >> 2Now the output will be 15 i.e.15 = 0000 1111

Loop Through a Chash Array

karthikeya Boyini
Updated on 20-Jun-2020 10:03:49

6K+ Views

To loop through an array in C#, use any of the loops. These loops have starting and ending value set that allows you to set or check value through iterations.C# has while, do…while, for and foreach loops to loop through an array.Let us see an example of for loop in C# −Example Live Demousing System; namespace ArrayApplication {    class MyArray {       static void Main(string[] args) {          int [] n = new int[10];          int i, j;          for ( i = 0; i < 10; ... Read More

Concatenate Two Arrays in C#

Samual Sam
Updated on 20-Jun-2020 10:02:38

547 Views

To concatenate two arrays in C#, let us first declare and initialize the array. Here, we have considered a string array −string[] str = new string[] { "Hello","World" };Now let us use the join() method to concatenate −.string.Join(" ", str);Now let us see the complete code to concatenate two arrays.Example Live Demousing System; class Program {    static void Main() {       string[] str = new string[] { "Hello","World" };       string res = string.Join(" ", str);       Console.WriteLine(res);    } }OutputHello World

Advertisements