Found 2587 Articles for Csharp

C# program to print all the numbers divisible by 3 and 5 for a given number

Samual Sam
Updated on 20-Jun-2020 14:36:21

6K+ Views

To print the numbers divisible by 3 and 5, use the && operator and check two conditions −f (num % 3 == 0 && num % 5 == 0) {}If the above condition is true, that would mean the number is divisible by 3 as well as 5.The following is the complete 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 num;          num = 15;          Console.WriteLine("Number: "+num);          // checking if the number is divisible by 3 and 5          if (num % 3 == 0 && num % 5 == 0) {             Console.WriteLine("Divisible by 3 and 5");          } else {             Console.WriteLine("Not divisible by 3 and 5");          }          Console.ReadLine();       }    } }OutputNumber: 15 Divisible by 3 and 5

C# program to print all the common elements of two lists

karthikeya Boyini
Updated on 20-Jun-2020 14:37:48

2K+ Views

Firstly create the two lists −List list1 = new List() {40, 20, 60, 3, 55}; List list2 = new List() {20, 70, 55, 80};To find the common elements, use the Intersect −list1.Intersect(list2)The following is the complete code to find the common elements between the two lists −Example Live Demousing System; using System.Linq; using System.Collections.Generic; namespace Demo {    class Program {       static void Main(string[] args) {          List list1 = new List() {40, 20, 60, 3, 55};          List list2 = new List() {20, 70, 55, 80};          Console.WriteLine("Common elements:");          foreach(int value in list1.Intersect(list2))          Console.WriteLine(value);       }    } }OutputCommon elements: 20 55

How to write multi-line comments in C#?

Samual Sam
Updated on 20-Jun-2020 14:40:21

875 Views

The comments that spread more than one line is called multi-line comments −/* The following is a multi-line Comment In C# /*The /*...*/ is ignored by the compiler and it is put to add comments in the program.The following is a sample C# program showing how to add MULTI-line comments −using System; namespace Demo {    class Program {       static void Main(string[] args) {          /* The following is a multi-line          Comment In C#          /*          // printing          Console.WriteLine("Hello World");          Console.ReadKey();       }    } }

How to get int value from enum in C#?

karthikeya Boyini
Updated on 20-Jun-2020 14:41:32

238 Views

Firstly, set the enum −public enum Vehicle { Car, Bus, Truck }Now use typecasting to cast an enum to int −int a = (int)Vehicle.Car;The following is the complete code to cast an enum to int −Example Live Demousing System; public class Demo {    public enum Vehicle { Car, Bus, Truck }    public static void Main() {       int a = (int)Vehicle.Car;       int b = (int)Vehicle.Bus;       int c = (int)Vehicle.Truck;       Console.WriteLine("Car = {0}", a);       Console.WriteLine("Bus = {0}", b);       Console.WriteLine("Truck = {0}", c);    } }OutputCar = 0 Bus = 1 Truck = 2

What is garbage collection in C#?

Chandu yadav
Updated on 20-Jun-2020 14:40:47

9K+ Views

The garbage collector (GC) manages the allocation and release of memory. The garbage collector serves as an automatic memory manager.You do not need to know how to allocate and release memory or manage the lifetime of the objects that use that memory.An allocation is made any time you declare an object with a “new” keyword or a value type is boxed. Allocations are typically very fast.When there isn’t enough memory to allocate an object, the GC must collect and dispose of garbage memory to make memory available for new allocations.This process is known as garbage collection.Garbage Collection in C# has ... Read More

How to calculate power of three using C#?

George John
Updated on 20-Jun-2020 14:13:57

323 Views

For power of 3, se the power as 3 and apply a recursive code like the following snippet −if (p!=0) { return (n * power(n, p - 1)); }Let’s say the number is 5, then the iterations would be −power(5, 3 - 1)); // 25 power (5,2-1): // 5The above would return5*25 i.e 125 as shown below −Example Live Demousing System; using System.IO; public class Demo {    public static void Main(string[] args) {       int n = 5;       int p = 3;       long res;       res = power(n, p);       Console.WriteLine(res);    }    static long power (int n, int p) {       if (p!=0) {          return (n * power(n, p - 1));       }       return 1;    } }Output125

What is the Values property of SortedList class in C#?

Samual Sam
Updated on 20-Jun-2020 14:15:10

91 Views

Firstly, declare the SortedList class −SortedList list = new SortedList();Now add the values −list.Add("S1", "Wallets"); list.Add("S2", "Sunglasses"); list.Add("S3", "Backpacks");The following is the code to work with Values property of SortedList class −Example Live Demousing System; using System.Collections; namespace Demo {    class Program {       static void Main(string[] args) {          SortedList list = new SortedList();          list.Add("S1", "Wallets");          list.Add("S2", "Sunglasses");          list.Add("S3", "Backpacks");          foreach (string value in list.Values) {             Console.WriteLine(value);          }       }    } }OutputWallets Sunglasses Backpacks

What is the Values property of Hashtable class in C#?

Ankith Reddy
Updated on 20-Jun-2020 14:15:50

126 Views

The Values property gets an ICollection containing the values in the Hashtable.Declare Hashtable collection −Hashtable ht = new Hashtable();Now add valuesht.Add("One", "Henry"); ht.Add("Two", "Kevin"); ht.Add("Three", "David");To display values from Hashtable, the following is the code −Example Live Demousing System; using System.Collections; namespace Demo {    class Program {       static void Main(string[] args) {          Hashtable ht = new Hashtable();          ht.Add("One", "Henry");          ht.Add("Two", "Kevin");          ht.Add("Three", "David");          // Displaying values          foreach (string value in ht.Values) {             Console.WriteLine(value);          }          Console.ReadKey();       }    } }OutputDavid Henry Kevin

What is the use of ‘new’ keyword in C#?

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

860 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

Fibonacci Series in C#

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

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

Advertisements