Assign Same Value to Multiple Variables in C#

Ankith Reddy
Updated on 20-Jun-2020 14:35:30

3K+ Views

To assign same value to multiple variables in a single line, use the = operator −val1 = val2 = 20;The above statement assigns 20 to the variables val1 and val2 as shown in the following code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo {    class MyApplication {       static void Main(string[] args) {          int val1, val2;          val1 = val2 = 20;          Console.WriteLine("Value1 = "+val1);          Console.WriteLine("Value2 = "+val2);       }    } }OutputValue1 = 20 Value2 = 20

Print All Sublists of a List in C#

karthikeya Boyini
Updated on 20-Jun-2020 14:34:45

705 Views

Firstly, create a list −List list = new List();The string here is “xyz” for which we will find the sublists. While looping we will declare another list, that would generate sublists on every true iteration −for (int i = 1; i < str.Length; i++) {    list.Add(str[i - 1].ToString());    List newlist = new List();    for (int j = 0; j < list.Count; j++) {       string list2 = list[j] + str[i];       newlist.Add(list2);    }    list.AddRange(newlist); }The following is the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo ... Read More

Check if a Hash List is Empty

George John
Updated on 20-Jun-2020 14:34:01

3K+ Views

Use the Any method to find whether the list is empty or not.Set the list −var subjects = new List(); subjects.Add("Maths"); subjects.Add("Java"); subjects.Add("English"); subjects.Add("Science"); subjects.Add("Physics"); subjects.Add("Chemistry");Now set the following condition to check whether the list is empty or not −bool isEmpty = !subjects.Any(); if(isEmpty) {       Console.WriteLine("Empty");    }else {       Console.WriteLine("List is not empty");    }The following is the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main(string[] args) {       var subjects = new List();       subjects.Add("Maths");       ... Read More

Write a Hash Function to Print Nth Number in Fibonacci Series

Samual Sam
Updated on 20-Jun-2020 14:25:20

686 Views

Set the following, if the nth number is let’s say num −int n = num- 1; int[] val = new int[n + 1];Then set the default Fibonacci numbers on the first and second position −val[0]= 0; val[1]= 1;Loop through i=2 to i

Types of Loops Supported in C#

George John
Updated on 20-Jun-2020 14:22:16

179 Views

A loop statement allows us to execute a statement or a group of statements multiple times. The following are the loops supported in C# −Sr.NoLoop Type & Description1while loopIt repeats a statement or a group of statements while a given condition is true. It tests the condition before executing the loop body.2for loopIt executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.3do...while loopIt is similar to a while statement, except that it tests the condition at the end of the loop bodyWith C#, you can also use foreach loop as shown below −Example Live ... Read More

Rotate Div with Matrix Transforms Using CSS

Srinivas Gorla
Updated on 20-Jun-2020 14:22:10

299 Views

You can try to run the following code to rotate div with matrix transforms using CSS:ExampleLive Demo                    div {             width: 300px;             height: 100px;             background-color: pink;             border: 1px solid black;          }          div#myDiv1 {             /* IE 9 */             -ms-transform: matrix(1, -0.3, 0, 1, 0, 0);             /* Safari */             -webkit-transform: matrix(1, -0.3, 0, 1, 0, 0);             /* Standard syntax */             transform: matrix(1, -0.3, 0, 1, 0, 0);          }                              Welcome to my website.                      Welcome to my website.           Output

Use of sizeof Operator in C#

karthikeya Boyini
Updated on 20-Jun-2020 14:20:00

461 Views

The sizeof() datatype returns the size of a data type. Let’s say you need to find the size of int datatype −sizeof(int);For double datatype −sizeof(double);Let us see the complete example to find the size of various datatypes −Example Live Demousing System; namespace Demo {    class Program {       static void Main(string[] args) {          Console.WriteLine("The size of long is {0}", sizeof(long));          Console.WriteLine("The size of double is {0}", sizeof(double));          Console.ReadLine();       }    } }OutputThe size of long is 8 The size of double is 8

Calculate Power Exponent Value Using C#

Samual Sam
Updated on 20-Jun-2020 14:18:25

2K+ Views

To calculate the power exponent value, use the Math.pow() method.Here, n is the number and p is the power −double res = Math.Pow(n, p);The following is the complete code −Example Live Demousing System; class Program {    static void Main() {       double n, p;       n = 7;       p = 3;       Console.WriteLine("Exponent Power= "+n);       double res = Math.Pow(n, p);       Console.WriteLine("Result= {0}", res);       Console.ReadLine();    } }OutputExponent Power= 7 Result= 343

Fibonacci Series in C#

Arjun Thakur
Updated on 20-Jun-2020 14:17:48

8K+ Views

To find Fibonaccli series, firsty set the first two number in the series as 0 and 1.int val1 = 0, val2 = 1, vNow loop through 2 to n and find the fibonai series. Every number in the series is the sum of the last 2 elements −for(i=2;i

Use of New Keyword in C#

karthikeya Boyini
Updated on 20-Jun-2020 14:16:55

896 Views

Use the new keyword to create an instance of the array −int [] a = new int[5];The new operator is used to create an object or instantiate an object. Here in the example, an object is created for the class using the new −Example Live Demousing System; namespace CalculatorApplication {    class NumberManipulator {       public void swap(int x, int y) {          int temp;          temp = x; /* save the value of x */          x = y; /* put y into x */     ... Read More

Advertisements