Found 2745 Articles for Csharp

C# Program to Check status of Current Thread

Samual Sam
Updated on 19-Jun-2020 08:37:16

2K+ Views

To check the status of the current thread in C#, use the IsAlive property.Firstly, use the currentThread property to display information about a thread −Thread thread = Thread.CurrentThread;Now use the thread.IsAlive property to check the status of the thread −thread.IsAliveExampleLet us see the complete code to check the status of current thread in C#.Live Demousing System; using System.Threading; namespace Demo {    class MyClass {       static void Main(string[] args) {          Thread thread = Thread.CurrentThread;          thread.Name = "My New Thread";          Console.WriteLine("Thread Status = {0}", thread.IsAlive);          Console.ReadKey();       }    } }OutputThread Status = True

C# Program to Convert Integer to String

karthikeya Boyini
Updated on 02-Sep-2023 15:26:43

39K+ Views

To convert an integer to string in C#, use the ToString() method.Set the integer for which you want the string −int num = 299;Use the ToString() method to convert Integer to String −String s; int num = 299; s = num.ToString();ExampleYou can try to run the following code to convert an integer to string in C# −Live Demousing System; class MyApplication {    static void Main(string[] args) {       String s;       int num = 299;       s = num.ToString();       Console.WriteLine("String = "+s);       Console.ReadLine();    } }OutputString = 299

C# program to check if there are K consecutive 1’s in a binary number

Samual Sam
Updated on 19-Jun-2020 08:37:46

188 Views

To check for consecutive 1’s in a binary number, you need to check for 0 and 1.Firstly, set a bool array for 0s and 1s i.e. false and true −bool []myArr = {false, true, false, false, false, true, true, true};For 0, set the count to 0 −if (myArr[i] == false)    count = 0;For 1, increment the count and set the result. The Max() method returns the larger of two number −count++; res = Math.Max(res, count);ExampleThe following is the example to check if there are K consecutive 1’s in a binary number −Live Demousing System; class MyApplication {    static ... Read More

C# program to check if binary representation is palindrome

Samual Sam
Updated on 19-Jun-2020 08:15:05

183 Views

To check for palindrome, let us say our number is 5, whose binary is −101The palindrome of 101 is 101 and to check you need to reverse the bits using the following function. Here, bitwise left and bitwise right shift operators are used −public static long funcReverse(long num) {    long myRev = 0;    while (num > 0) {       myRev = 1;    }    return myRev; }Then the actual representation will be compared be the reverse one by returning and getting the value from the funcReverse() function −public static bool checkPalindrome(long num) {    long ... Read More

Bitwise right shift operators in C#

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

538 Views

Bitwise operator works on bits and performs bit by bit operation. In Bitwise right shift operator the value of the left operand is moved right by the number of bits specified by the right operand.In the below code, we have the value −60 i.e. 0011 1100On the right shift %minus;c = a >> 2;It converts into 15 after right shift twice −15 i.e. 0000 1111ExampleYou can try to run the following code to implement Bitwise right shift operator in C# −using System; using System.Collections.Generic; using System.Text; namespace Demo {    class toBinary {       static void Main(string[] args) ... Read More

Binary to decimal using C#

Samual Sam
Updated on 19-Jun-2020 08:18:40

133 Views

To convert binary to decimal, here I have used a while loop and found the remainder of the Binary number, which is the input. After that, the remainder is multiplied by the base value and added.This is what I did to get the decimal value −while (val > 0) {    remainder = val % 10;    myDecimal = myDecimal + remainder* baseVal;    val = val / 10;    baseVal = baseVal * 2; }ExampleLet us see the complete code to convert binary to decimal in C# −Live Demousing System; using System.Collections.Generic; using System.Text; namespace Demo {    class ... Read More

Binary search in C#

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

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

374 Views

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

712 Views

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

Advertisements