Found 34488 Articles for Programming

What are control statements in C#?

Samual Sam
Updated on 20-Jun-2020 09:59:29

6K+ Views

The flow of program control is specified by control statements in C#. It includes the following −if statementAn if statement consists of a boolean expression followed by one or more statements.The following is the syntax −if(boolean_expression) {    /* statement(s) will execute if the boolean expression is true */ }if-else statementAn if statement can be followed by an optional else statement, which executes when the boolean expression is false.The following is the syntax −if(boolean_expression) {    /* statement(s) will execute if the boolean expression is true */ } else {    /* statement(s) will execute if the boolean expression is ... Read More

How to concatenate Two Arrays in C#?

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

342 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

How do you loop through a C# array?

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

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

How to use RightShift Operators in C#?

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

100 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

How to use the sleep method in C#?

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

698 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

Write a C# program to check if a number is prime or not

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

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

Write a C# program to check if a number is divisible by 2

Samual Sam
Updated on 20-Jun-2020 09:54:42

2K+ Views

To check if a number is divisible by2 or not, you need to first find the remainder.If the remainder of the number when it is divided by 2 is 0, then it would be divisible by 2.Let’s say our number is 10, we will check it using the following if-else −// checking if the number is divisible by 2 or not if (num % 2 == 0) {    Console.WriteLine("Divisible by 2 "); } else {    Console.WriteLine("Not divisible by 2"); }The following is an example to find whether the number is divisible by 2 or not −Example Live Demousing System; ... Read More

How to use ‘is’ operator in C#?

karthikeya Boyini
Updated on 20-Jun-2020 09:56:17

110 Views

The "is" operator in C# checks whether the run-time type of an object is compatible with a given type or not.The following is the syntax −expr is typeHere, expr is the expression and type is the name of the typeThe following is an example showing the usage of is operator in C# −Example Live Demousing System; class One { } class Two { } public class Demo {    public static void Test(object obj) {       One x;       Two y;       if (obj is One) {          Console.WriteLine("Class One");   ... Read More

How to use ‘as’ operator in C#?

Samual Sam
Updated on 20-Jun-2020 09:57:12

241 Views

The "as" operator perform conversions between compatible types. It is like a cast operation and it performs only reference conversions, nullable conversions, and boxing conversions. The as operator can't perform other conversions, such as user-defined conversions, which should instead be performed by using cast expressions.The following is an example showing the usage of as operation in C#. Here ‘as’ is used for conversion:string s = obj[i] as string;Try to run the following code to work with ‘as’ operator in C# −Example Live Demousing System; public class Demo {    public static void Main() {       object[] obj = ... Read More

Write a C# program to calculate a factorial using recursion

karthikeya Boyini
Updated on 20-Jun-2020 09:58:04

2K+ Views

Factorial of a number is what we are finding using a recursive function checkFact () in the below example −If the value is 1, it returns 1 since Factorial is 1 −if (n == 1) return 1;If not, then the recursive function will be called for the following iterations if you want the value of 5!Interation1: 5 * checkFact (5 - 1); Interation2: 4 * checkFact (4 - 1); Interation3: 3 * checkFact (3 - 1); Interation4: 4 * checkFact (2 - 1);To calculate a factorial using recursion, you can try to run the following code which ... Read More

Advertisements