- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 34494 Articles for Programming
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
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:Example Live Demousing System; using System.Collections.Generic; public class Demo { public static void Main(string[] args) { int n = 19; ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
2K+ 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 −Example Live Demousing 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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
310 Views
Firstly, use the DirectoryInfo object −//creating a DirectoryInfo object DirectoryInfo mydir = new DirectoryInfo(@"d:\amit");Now, use the GetFiles() method to get all the files −FileInfo [] f = mydir.GetFiles();To get the list of files in a directory, try to run the following code −Exampleusing System; using System.IO; namespace Demo { class Program { static void Main(string[] args) { //creating a DirectoryInfo object DirectoryInfo mydir = new DirectoryInfo(@"d:\amit"); // getting the files in the directory, their names and size FileInfo [] f = mydir.GetFiles(); foreach (FileInfo file in f) { Console.WriteLine("File Name: {0} Size: {1}", file.Name, file.Length); } Console.ReadKey(); } } }
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
2K+ 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 −Example Live Demousing 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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
352 Views
Firstly, add elements −IDictionary d = new Dictionary(); d.Add(1,97); d.Add(2,89); d.Add(3,77); d.Add(4,88);Now, get the keys −List myList = new List(d.Keys);To iterate −foreach (int k in myList) { Console.WriteLine("{0}, {1}", k, d[k]); }The following is an example −Example Live Demousing System; using System.Collections.Generic; public class Demo { public static void Main() { IDictionary < int, int > d = new Dictionary < int, int > (); d.Add(1, 97); d.Add(2, 89); d.Add(3, 77); d.Add(4, 88); List < int > myList = new List < int > (d.Keys); foreach(int k in myList) { Console.WriteLine("{0}, {1}", k, d[k]); } } }Output1, 97 2, 89 3, 77 4, 88
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
372 Views
To iterate efficiently through an array of integers of unknown size in C# is easy. Let’s see how.Firstly, set an array, but do not set the size −int[] arr = new int[] { 5, 7, 2, 4, 1 };Now, get the length and iterate through an array using for loop −for (int i = 0; i< arr.Length; i++) { Console.WriteLine(arr[i]); }Let us see the complete example −Example Live Demousing System; public class Program { public static void Main() { int[] arr = new int[] { 5, 7, 2, 4, 1 }; // Length Console.WriteLine("Length:" + arr.Length); for (int i = 0; i < arr.Length; i++) { Console.WriteLine(arr[i]); } } }OutputLength:5 5 7 2 4 1
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
1K+ Views
C# has no built-in Math type. For the same, use a Dictionary.Firstly, create a Dictionary −Dictionary d = new Dictionary(); d.Add("keyboard", 1); d.Add("mouse", 2);Get the keys −var val = d.Keys.ToList();Now, use the foreach loop to iterate over the Map −foreach (var key in val) { Console.WriteLine(key); }To iterate it, try to run the following code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { Dictionary d = new Dictionary(); d.Add("keyboard", 1); d.Add("mouse", 2); // get keys ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
4K+ Views
Use the new operator to instantiate a class in C#.Let’s say our class is Line. Instantiation will create a new object as shown below −Line line = new Line();Using the object, you can now call the method −line.setLength(6.0);Let us see the example −Example Live Demousing System; namespace LineApplication { class Line { private double length; // Length of a line public Line() { Console.WriteLine("Object is being created"); } public void setLength( double len ) { length = len; ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
402 Views
To insert an item in an already created ArrayList, use the Insert() method.Firstly, set elements −ArrayList arr = new ArrayList(); arr.Add(45); arr.Add(78); arr.Add(33);Now, let’s say you need to insert an item at 2nd position. For that, use the Insert() method −// inserting element at 2nd position arr.Insert(1, 90);Let us see the complete example −Example Live Demousing System; using System.Collections; namespace Demo { public class Program { public static void Main(string[] args) { ArrayList arr = new ArrayList(); arr.Add(45); arr.Add(78); ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
2K+ Views
Use a while loop to input multiple values from the user in one line.Let’s say you need to get the elements of a matrix. Get it using Console.ReadLine() as shown below −Console.Write("Enter elements - Matrix 1 : "); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { arr1[i, j] = Convert.ToInt16(Console.ReadLine()); } }The following is an example showing how we can input multiple values from user −Example Live Demousing System; namespace Demo { public class Program { public static void Main(string[] args) ... Read More