Found 34494 Articles for Programming

Calculate the execution time of a method in C#

Samual Sam
Updated on 22-Jun-2020 10:09:06

632 Views

Use Stopwatch class to measure time of execution of a method in .NET −Stopwatch s = Stopwatch.StartNew();Now set a function and use the ElapsedMilliseconds property to get the execution time in milliseconds −s.ElapsedMillisecondsLet us see the complete code −Example Live Demousing System; using System.IO; using System.Diagnostics; public class Demo {    public static void Main(string[] args) {       Stopwatch s = Stopwatch.StartNew();       display();       for (int index = 0; index < 5; index++) {          Console.WriteLine("Time taken: " + s.ElapsedMilliseconds + "ms");       }       ... Read More

Find power of a number using recursion in C#

George John
Updated on 22-Jun-2020 10:09:37

169 Views

To find the power of a number, firstly set the number and the power −int n = 15; int p = 2;Now create a method and pass these values −static long power(int n, int p) {    if (p != 0) {       return (n * power(n, p - 1));    }    return 1; } Above, the recursive call gave us the results −n * power(n, p - 1)The following is the complete code to get the power of a number −Example Live Demousing System; using System.IO; public class Demo {    public static void Main(string[] args) { ... Read More

How to find the Current Context id of the Thread in C#?

Samual Sam
Updated on 22-Jun-2020 10:11:35

531 Views

To create a new thread.Thread thread = Thread.CurrentThread; thread.Name = "My new Thread”;To get the current context id, use the ContextID property.Thread.CurrentContext.ContextIDLet us see the complete code −Example 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 Name = {0}", thread.Name);          Console.WriteLine("current Context id: {0}", Thread.CurrentContext.ContextID);          Console.ReadKey();       }    } }OutputThread Name = My new Thread current Context id: 0

How to get the nth value of a Fibonacci series using recursion in C#?

Chandu yadav
Updated on 22-Jun-2020 10:13:04

1K+ Views

Create a method to get the nth value with recursion.public int displayFibonacci(int n)Call the method −displayFibonacci(val)On calling, the displayFibonacci() meyhod gets called and calculate the nth value using recursion.public int displayFibonacci(int n) {    if (n == 0) {       return 0;    }    if (n == 1) {       return 1;    } else {       return displayFibonacci(n - 1) + displayFibonacci(n - 2);    } }Let us see the complete code −Example Live Demousing System; public class Demo {    public static void Main(string[] args) {       Demo d = ... Read More

How to find the Sum of two Binary Numbers using C#?

karthikeya Boyini
Updated on 22-Jun-2020 10:13:48

2K+ Views

To find the sum of two binary numbers, firstly set them.val1 = 11110; val2 = 11100;Now call the displaySum() method, which created to display the sumL.sum = displaySum(val1, val2);We have set a new array in the method to display each bit of the binary number.long[] sum = new long[30];Now let us see the complete code to calculate the sum of binary numbers as shown in the code below −Example Live Demousing System; class Demo {    public static void Main(string[] args) {       long val1, val2, sum = 0;       val1 = 11110;       val2 ... Read More

Find frequency of each word in a string in C#

Samual Sam
Updated on 22-Jun-2020 10:14:23

627 Views

To find the frequency of each word in a string, you can try to run the following code −Example Live Demousing System; class Demo {    static int maxCHARS = 256;    static void calculate(String s, int[] cal) {       for (int i = 0; i < s.Length; i++)       cal[s[i]]++;    }    public static void Main() {       String s = "Football!";       int []cal = new int[maxCHARS];       calculate(s, cal);       for (int i = 0; i < maxCHARS; i++)       if(cal[i] > ... Read More

How to find the size of a variable without using sizeof in C#?

Syed Javed
Updated on 23-Jun-2020 08:17:47

2K+ Views

To get the size of a variable, sizeof is used.int x; x = sizeof(int);To get the size of a variable, without using the sizeof, try the following code −// without using sizeof byte[] dataBytes = BitConverter.GetBytes(x); int d = dataBytes.Length;Here is the complete code.Example Live Demousing System; class Demo {    public static void Main() {       int x;       // using sizeof       x = sizeof(int);       Console.WriteLine(x);       // without using sizeof       byte[] dataBytes = BitConverter.GetBytes(x);       int d = dataBytes.Length;       Console.WriteLine(d);    } }Output4 4

Declare a const array in C#

Ankith Reddy
Updated on 22-Jun-2020 09:57:59

8K+ Views

In C#, use readonly to declare a const array.public static readonly string[] a = { "Car", "Motorbike", "Cab" };In readonly, you can set the value at runtime as well unlike const.Another alternative of achieving what we saw above −public ReadOnlyCollection a { get { return new List { "Car", "Motorbike", "Cab" }.AsReadOnly();}} .NET framework 4.5 brings an improvement to what we saw −public ReadOnlyCollection a { get; } = new ReadOnlyCollection( new string[] { "Car", "Motorbike", "Cab" } );

How to find whether the Number is Divisible by 2 using C#?

George John
Updated on 22-Jun-2020 09:59:28

200 Views

To find whether a number is divisibe by 2 is not, check what happens when the number is divisible by 2.If the remainder is 0, then the number is divisible by 2, else it is false −if (num % 2 == 0) {    Console.WriteLine("Divisible by 2 "); } else {    Console.WriteLine("Not divisible by 2"); }The following is the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo {    class Test {       static void Main(string[] args) {          int num;          num = 18; ... Read More

C# program to find the most frequent element

karthikeya Boyini
Updated on 22-Jun-2020 09:58:31

781 Views

Let’s say our string is −String s = "HeathLedger!";Now create a new array.int []cal = new int[maxCHARS];Create a new method and pass both the string and the new array in it. Find the maximum occurrence of a character.static void calculate(String s, int[] cal) {    for (int i = 0; i < s.Length; i++)    cal[s[i]]++; }Let us see the complete code −Example Live Demousing System; class Demo {    static int maxCHARS = 256;    static void calculate(String s, int[] cal) {       for (int i = 0; i < s.Length; i++)       cal[s[i]]++;    } ... Read More

Advertisements