Found 2749 Articles for Csharp

Binary search in C#

karthikeya Boyini
Updated on 19-Jun-2020 08:20:38

7K+ Views

Binary search works on a sorted array. The value is compared with the middle element of the array. If equality is not found, then the half part is eliminated in which the value is not there. In the same way, the other half part is searched.Here is the mid element in our array. Let’s say we need to find 62, then the left part would be eliminated and the right part is then searched −These are the complexities of a binary search −Worst-case performanceO(log n)Best-case performanceO(1)Average performanceO(log n)Worst-case space complexityO(1)ExampleLet us see the method to implement the binary search −public ... Read More

BigInteger Class in C#

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


Use the BigInteger to handle big numbers in C#. The assembly to add for BigInteger is System. Numerics.In c# Big integer is found in System.Numerics.BigInteger.SyntaxThe syntax of BigInteger −[SerializableAttribute] public struct BigInteger : IFormattable, IComparable, IComparable, IEquatableLet us see an example code snippet −BigInteger num = BigInteger.Multiply(Int64.MaxValue, Int64.MaxValue);You can create BigInteger like this −BigInteger num = new BigInteger(double.MaxValue);The following are some of its constructors −S.No.Constructor & Description1BigInteger(Byte[ ])A new instance of the BigInteger structure using the values in a byte array.2BigInteger(Decimal)A new instance of the BigInteger structure using a Decimal value.            3BigInteger(Double)A new instance of ... Read More

Basic calculator program using C#

karthikeya Boyini
Updated on 19-Jun-2020 08:24:03


To create a calculator program in C#, you need to use Web Forms. Under that create buttons from 1-9, addition, subtraction, multiplication, etc.Let us see the code for addition, subtraction, and multiplication. Firstly, we have declared two variables −static float x, y;Now, we will see how to set codes for calculation on individual button click: Our result textbox is tbResult since we have used Windows Form as well to display the calculator −protected void add_Click(object sender, EventArgs e) {    x = Convert.ToInt32(tbResult.Text);    tbResult.Text = "";    y = '+';    tbResult.Text += y; } protected void sub_Click(object sender, ... Read More

Background Worker Class in C#

Samual Sam
Updated on 19-Jun-2020 08:24:59

1K+ Views

As the name suggests the Background Worker Class allows you to set a thread continuously running in the background and communicating with the main thread whenever required.BackgroundWorker makes the implementation of threads in Windows Forms. Intensive tasks need to be done on another thread so the UI does not freeze. It is necessary to post messages and update the user interface when the task is done. The following properties are used in BackgroundWorker class:Reference: Microsoft Developer Network (MSDN)S.No.Name & Description1CancellationPendingA value indicating whether the application has requested cancellation of a background operation.2CanRaiseEventsGets a value indicating whether the component can raise an ... Read More

Background and foreground thread in C#

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


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


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

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


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