Csharp Articles

Page 91 of 196

How to join or concatenate two lists in C#?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 3K+ Views

To concatenate two lists, use AddRange() method.Set the first list −var products1 = new List < string > (); products1.Add("Belts"); products1.Add("Tshirt"); products1.Add("Trousers");Set the second list −var products2 = new List < string > (); products2.Add("Footwear"); products2.Add("Electronics");To concatenate both the lists −products1.AddRange(products2);The following is the complete code −Exampleusing System.Collections.Generic; using System; namespace Demo {    public static class Program {       public static void Main() {          var products1 = new List < string > ();          products1.Add("Belts");          products1.Add("Tshirt");          products1.Add("Trousers");       ...

Read More

How to loop through all values of an enum in C#?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 3K+ Views

To loop through all the values of enum, use the Enum.GetValues().Firstly, set an Enum −public enum Grade { A, B, C, D, E, F };Now, with foreach loop, you need to loop through Enum.GetValues(typeof(Grade)) −foreach (Grade g in Enum.GetValues(typeof(Grade))) {    Console.WriteLine(g); }Here is the complete code −Exampleusing System; public class EnumExample {    public enum Grade { A, B, C, D, E, F };    public static void Main() {       foreach (Grade g in Enum.GetValues(typeof(Grade))) {          Console.WriteLine(g);       }    } }OutputA B C D E F

Read More

C# Program to check if a number is Positive, Negative, Odd, Even, Zero

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 2K+ Views

Check for the following conditions −For odd and even, check for the remainder when the number is divided by 2 −// checking for odd/ even if(n % 2 == 0) {    Console.WriteLine("Even"); } else {    Console.WriteLine("Odd"); }For positive, negative and checking whether a number is 0 or not −if (n < 0) { Console.WriteLine("Negative Number!"); } else if(n == 0) {    Console.WriteLine("Zero"); } else {    Console.WriteLine("Positive Number!"); }The following is the complete code:Exampleusing System; using System.Collections.Generic; public class Demo {    public static void Main(string[] args) {       int n = 19; ...

Read More

How to initialize a string to an empty string in C#?

Ankith Reddy
Ankith Reddy
Updated on 11-Mar-2026 3K+ Views

To initialize a string to an empty list −string myStr = null;Now, use the built-in method IsNullOrEmpty() to check whether the list is empty or not −if (string.IsNullOrEmpty(myStr)) {    Console.WriteLine("String is empty or null!"); }Let us see the complete code −Exampleusing System; namespace Demo {    class Program {       static void Main(string[] args) {          string myStr = null;          if (string.IsNullOrEmpty(myStr)) {             Console.WriteLine("String is empty or null!");          }          Console.ReadKey();       } ...

Read More

How to get last 4 characters from string in\\nC#?

radhakrishna
radhakrishna
Updated on 11-Mar-2026 3K+ Views

Firstly, set the string −string str = "Football and Tennis";Now, use the substring() method to get the last 4 characters −str.Substring(str.Length - 4);Let us see the complete code −Exampleusing System; public class Demo {    public static void Main() {       string str = "Football and Tennis";       string res = str.Substring(str.Length - 4);       Console.WriteLine(res);    } }Outputnnis

Read More

How to empty a C# list?

Giri Raju
Giri Raju
Updated on 11-Mar-2026 14K+ Views

To empty a C# list, use the Clear() method.Firstly, set a list and add elements −List myList = new List() {    "one",    "two",    "three",    "four",    "five",    "six" };Now, let us empty the list −myList.Clear();Exampleusing System; using System.Collections.Generic; public class Program {    public static void Main() {       List myList = new List() {          "one",          "two",          "three",          "four",          "five",          "six"       };       foreach(string ...

Read More

How do I determine the size of my array in C#

seetha
seetha
Updated on 11-Mar-2026 243 Views

Firstly, set an array −int[] arr = {6, 3, 8, 4};Now, use the Length property to get the size of the array −arr.LengthLet us see the complete code −Exampleusing System; namespace Demo {    public class Demo {       public static void Main(string[] args) {          int[] arr = {6, 3, 8, 4};          Console.WriteLine("Array...");          foreach (int i in arr) {             Console.Write(i + " ");          }          Console.WriteLine("Size of Array: "+arr.Length);       }    } }OutputArray... 6 3 8 4 Size of Array: 4

Read More

Class and Static Variables in C#

Arjun Thakur
Arjun Thakur
Updated on 11-Mar-2026 9K+ Views

Static variables are used for defining constants because their values can be retrieved by invoking the class without creating an instance of it. Static variables can be initialized outside the member function or class definition. You can also initialize static variables inside the class definition.Exampleusing System; namespace StaticVarApplication {    class StaticVar {       public static int num;       public void count() {          num++;       }       public int getNum() {          return num;       }    }    class StaticTester ...

Read More

Does declaring an array create an array in C#?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 133 Views

Declaring an array does not initialize the array in the memory. When the array variable is initialized, you can assign values to the array.The following is a declaration and it will not create an array −int[] id;The following create an array of integers. The array is a reference type, so you need to use the new keyword to create an instance of the array −Int[] id = new int[5] {};Let us see an example −Exampleusing System; namespace ArrayApplication {    public class MyArray {       public static void Main(string[] args) {          int [] ...

Read More

C# ToEven property

Ankith Reddy
Ankith Reddy
Updated on 11-Mar-2026 144 Views

The ToEven property is used with the MidpointRounding Enumeration to round a number to the nearest even number.Declare and initialize a decimal number −decimal val = 70.45M;To rounde a number to the nearest even number −decimal.Round(val, 0, MidpointRounding.ToEven)Here is the complete code −Exampleusing System; using System.Linq; class Demo {    static void Main() {       decimal val = 70.45M;       Console.WriteLine(decimal.Round(val, 0, MidpointRounding.ToEven));    } }Output70

Read More
Showing 901–910 of 1,951 articles
« Prev 1 89 90 91 92 93 196 Next »
Advertisements