Csharp Articles

Page 112 of 196

How to write single-line comments in C#?

Samual Sam
Samual Sam
Updated on 20-Jun-2020 357 Views

If you want to add a comment that restricts itself to a single line, then use the single-line comments −// variable int i = 20;The following is a sample C# program showing how to add single-line comments −Example Live Demousing System; namespace Demo {    class Program {       static void Main(string[] args) {          // display text          Console.WriteLine("Hello World");          Console.ReadKey();       }    } }OutputHello World

Read More

How to get int value from enum in C#?

karthikeya Boyini
karthikeya Boyini
Updated on 20-Jun-2020 273 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

Read More

What is garbage collection in C#?

Chandu yadav
Chandu yadav
Updated on 20-Jun-2020 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 write multi-line comments in C#?

Samual Sam
Samual Sam
Updated on 20-Jun-2020 928 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();       }    } }

Read More

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

Samual Sam
Samual Sam
Updated on 20-Jun-2020 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

Read More

How to assign same value to multiple variables in single statement in C#?

Ankith Reddy
Ankith Reddy
Updated on 20-Jun-2020 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

Read More

C# program to print all sublists of a list

karthikeya Boyini
karthikeya Boyini
Updated on 20-Jun-2020 733 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

How to check if a C# list is empty?

George John
George John
Updated on 20-Jun-2020 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

How to calculate the power exponent value using C#?

Samual Sam
Samual Sam
Updated on 20-Jun-2020 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

Read More

Fibonacci Series in C#

Arjun Thakur
Arjun Thakur
Updated on 20-Jun-2020 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

Read More
Showing 1111–1120 of 1,951 articles
« Prev 1 110 111 112 113 114 196 Next »
Advertisements