Found 2745 Articles for Csharp

Background and foreground thread in C#

karthikeya Boyini
Updated on 19-Jun-2020 08:25:21

491 Views

A thread is defined as the execution path of a program. Each thread defines a unique flow of controlBackground ThreadsWhen the foreground threads will close, the background threads will be terminated.The property used for background thread is IsBackground that gets or sets a value indicating whether a thread is a background thread. The default value of this property would be false because the default threads created are Foreground Threads.To create a background thread −Thread bgThread = new Thread(tStart); bgThread.IsBackground = true; bgThread.Start();Foreground ThreadsForeground threads continue to run until the last foreground thread is terminated.When all the foreground threads are stopped, ... Read More

Boxing and Unboxing in C#

Samual Sam
Updated on 19-Jun-2020 08:25:57

1K+ Views

BoxingBoxing is the implicit conversion of a value type to a reference type.UnboxingUnboxing is the explicit conversion of the reference type created by boxing, back to a value type.ExampleLet us see an example code snippet −// int int myVal = 12; // Boxing object myBoxed = myVal; // Unboxing int myUnBoxed = (int) myBoxed;Let us see another example that shows an array list in C# −int a = 5; ArrayList arr = new ArrayList(); // Boxing arr.Add(a); // UnBoxing int b = (int)arr[0];

Async & await keyword in C#

karthikeya Boyini
Updated on 19-Jun-2020 08:26:51

790 Views

The async and await keyword is used in C# for asynchronous programming.An application with a GUI, check the content of the queue and if an unprocessed task is there, it takes it out and processes it first. The code executes synchronously and the unprocessed task is completed first. The application will show stop responding to messages if the processing takes more time than expected.Let us see what is discussed above −private void OnRequestDownload(object sender, RoutedEventArgs e) {    var req = HttpWebRequest.Create(_requestedUri);    var res = req.GetResponse(); }To solve the above issue, use the async and await keywords −private async ... Read More

Association, Composition and Aggregation in C#

Samual Sam
Updated on 19-Jun-2020 08:06:31

4K+ Views

Association in C#The association defines the relationship between an object in C#. An a one-to-one, one-to-many, many-to-one and many-to-many relationship can be defined between objects.For example, An Employee can be associated with multiple projects, whereas a project can have more than one employee.Composition in C#Under Composition, if the parent object is deleted, then the child object also loses its status.The composition is a special type of Aggregation and gives a part-of relationship.For example, A Car has an engine. If the car is destroyed, the engine is destroyed as well.Aggregation in C#Aggregation is a direct relation between objects in C#. It ... Read More

Array Copy in C#

karthikeya Boyini
Updated on 19-Jun-2020 08:07:16

5K+ Views

Use the array. copy method in C# to copy a section of one array to another.Our original array has 10 elements −int [] n = new int[10]; /* n is an array of 10 integers */Our new array that would copy a section of array 1 has 5 elements −int [] m = new int[5]; /* m is an array of 5 integers */The array.copy() method allow you to add the source and destination array. With that, include the size of the section of the first array that includes in the second array.ExampleYou can try to run the following to ... Read More

Three Different ways to calculate factorial in C#

Samual Sam
Updated on 19-Jun-2020 08:08:47

513 Views

To calculate a factorial in C#, you can use any of the following three ways −Calculate factorial with for loopExampleLive Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace factorial {    class Test {       static void Main(string[] args) {          int i, res;          int value = 5;          res = value;          for (i = value - 1; i >= 1; i--) {             res = res * i;          }       ... Read More

Abstract vs Sealed Classes vs Class Members in C#

karthikeya Boyini
Updated on 19-Jun-2020 08:09:12

979 Views

The abstract class includes abstract and non-abstract methods. You cannot instantiate an abstract class.The sealed class prevents inheritance and you cannot use it as a base class.Abstract ClassesTo declare an abstract class, you need to place the keyword abstract before the class definition. An example of class members in an abstract class can be the following that defines an abstract method −public abstract class Vehicle {    public abstract void display(); }The abstract method definition is followed by a semi-colon since it has no implementation.Sealed ClassesTo declare a sealed class, you need to place the keyword sealed before the class definition. ... Read More

Built-in Exceptions in C#

Samual Sam
Updated on 19-Jun-2020 08:10:13

296 Views

Exceptions are a problem that arises when a program executed. The following keyword handles exceptions in C#:tryA try block identifies a block of code for which particular exceptions is activated.CatchThe catch keyword indicates the catching of an exception.finallyExecute a given set of statements, whether an exception is thrown or not thrown.throwAn exception is thrown when a problem shows up in a program.ExampleLet us see an example to handle the error in a C# program −Live Demousing System; namespace MyErrorHandlingApplication {    class DivNumbers {       int result;       DivNumbers() {          result = ... Read More

Addition and Concatenation in C#

karthikeya Boyini
Updated on 19-Jun-2020 08:11:15

226 Views

To add and concatenate strings in C#, use the string. Concat method. The plus operator can also be used for the same purpose of concatenation.Plus Operatorstring str2 = "Hanks" + str1;ExampleLet us see an example of + operator to concatenate strings −Live Demousing System; class Program {    static void Main() {       string str1 = "Tom";       // concatenation       string str2 = "Hanks" + str1;       Console.WriteLine(str2);    } }OutputHanksTomString.concatstring str2 = string.Concat("Hanks", str1);ExampleLet us see an example of string.concat to concatenate strings in C# −Live Demousing ... Read More

abstract keyword in C#

Samual Sam
Updated on 19-Jun-2020 08:12:01

527 Views

The abstract keyword in C# is used for abstract classes. An abstract class in C# includes abstract and nonabstract methods. You cannot instantiate an abstract class.Example of an abstract class Vehicle and abstract method display() −public abstract class Vehicle {    public abstract void display(); }The abstract class has derived classes: Bus, Car, and Motorcycle. The following is an implementation of the Car derived class −public class Car : Vehicle {    public override void display() {       Console.WriteLine("Car");    } }ExampleThe following is an example of abstract classes in C# −Live Demousing System; public abstract class Vehicle ... Read More

Advertisements