- 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 34486 Articles for Programming
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
10K+ Views
The KeyValuePair class stores a pair of values in a single list with C#.Set KeyValuePair and add elements −var myList = new List(); // adding elements myList.Add(new KeyValuePair("Laptop", 20)); myList.Add(new KeyValuePair("Desktop", 40)); myList.Add(new KeyValuePair("Tablet", 60));Here is the code to learn how to work with KeyValuePair and display the keys and values −ExampleUsing System; using System.Collections.Generic; class Program { static void Main() { var myList = new List(); // adding elements myList.Add(new KeyValuePair("Laptop", 20)); myList.Add(new KeyValuePair("Desktop", 40)); myList.Add(new KeyValuePair("Tablet", 60)); foreach (var val in myList) { Console.WriteLine(val); } } }
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
12K+ Views
To convert a string to a long, use the Long.parse method in C# −Firstly, set a string −string str = "6987646475767";Now, convert it to long −long.Parse(str);Here is the complete code −Example Live Demousing System; using System.Linq; class Demo { static void Main() { string str = "6987646475767"; long res = long.Parse(str); Console.WriteLine(res); } }Output6987646475767
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
2K+ Views
To convert a string to a bool, use the Bool.parse method in C# −Firstly, set a string −string str = "false";Now, convert it to bool −bool.Parse(str);Here is the complete code −Example Live Demousing System; using System.Linq; class Demo { static void Main() { string str = "false"; bool res = bool.Parse(str); Console.WriteLine(res); } }OutputFalse
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
1K+ Views
ContainsKey is a Dictionary method in C# and check whether a key exists in the Dictionary or not.Declare a Dictionary and add elements −var dict = new Dictionary() { {"TV", 1}, {"Home Theatre", 2}, {"Amazon Alexa", 3}, {"Google Home", 5}, {"Laptop", 5}, {"Bluetooth Speaker", 6} };Now, let’s say you need to check for the existence of a particular element in the Dictionary. For that, use the ContainsKey() method −if (dict.ContainsKey("Laptop") == true) { Console.WriteLine(dict["Laptop"]); }The following is the code −Example Live Demousing System; using System.Collections.Generic; public class Demo { public static void Main() ... Read More
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
604 Views
The Ulong type in C# is an alias to the System.UInt64 type. It is an Unsigned integer.Set the Ulong type −ulong val = 7712059531;To get the minimum and maximum value for Ulong type −Console.WriteLine(ulong.MaxValue); Console.WriteLine(ulong.MinValue);Here is the complete code −Example Live Demousing System; using System.Threading; using System.Diagnostics; public class Demo { public static void Main() { ulong val = 7712059531; Console.WriteLine(val); // Max and Min values Console.WriteLine(ulong.MaxValue); Console.WriteLine(ulong.MinValue); // typeof Console.WriteLine(typeof(ulong)); } }Output7712059531 18446744073709551615 0 System.UInt64
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
99 Views
Set a DateTime object;DateTime dt = DateTime.Now;To get years in different formats, try the following −dt.ToString("yy") dt.ToString("yyy") dt.ToString("yyyy") dt.ToString("yyyyy")Here is the complete code −Example Live Demousing System; using System.Threading; using System.Diagnostics; public class Demo { public static void Main() { DateTime dt = DateTime.Now; // Today's date Console.WriteLine("Today = {0}", DateTime.Today); Console.WriteLine("Displaying current year in dufferent formats:"); Console.WriteLine(dt.ToString("yy")); Console.WriteLine(dt.ToString("yyy")); Console.WriteLine(dt.ToString("yyyy")); Console.WriteLine(dt.ToString("yyyyy")); } }OutputToday = 9/4/2018 12:00:00 AM Displaying current year in dufferent formats: 18 2018 2018 02018
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
3K+ Views
Use DateTime.Subtract to get the difference between two dates in C#.Firstly, set two dates −DateTime date1 = new DateTime(2018, 8, 27); DateTime date2 = new DateTime(2018, 8, 28);Use the Subtract method to get the difference −TimeSpan t = date2.Subtract(date1);The following is the complete code −Example Live Demousing System; using System.Threading; using System.Diagnostics; public class Demo { public static void Main() { DateTime date1 = new DateTime(2018, 8, 27); DateTime date2 = new DateTime(2018, 8, 28); // getting the difference TimeSpan t = date2.Subtract(date1); Console.WriteLine(t); Console.WriteLine("Days (Difference) = {0} ", t.TotalDays); Console.WriteLine("Minutes (Difference) = {0}", t.TotalMinutes); } }Output1.00:00:00 Days (Difference) = 1 Minutes (Difference) = 1440
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
369 Views
To set the timer information to zero, use the Stopwatch Restart method.Firstly, begin the Stopwatch −Stopwatch s = Stopwatch.StartNew();Then, use Restart() to set the timer to zero −s.Restart();Let us see the complete code −ExampleLive Demousing System; using System.Threading; using System.Diagnostics; public class Demo { public static void Main() { Stopwatch s = Stopwatch.StartNew(); Thread.Sleep(500); // restart s.Restart(); // begin again Thread.Sleep(500); Console.WriteLine(s.Elapsed); } }Output00:00:00.5004937
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
2K+ Views
Stopwatch is a class in C# to measure the elapsed time. Use it to calculate the time a function took to execute. It is found under System.Diagnostics.To get the elapsed time, firstly begin the Stopwatch −var sw = Stopwatch.StartNew();For elapsed ticks −long ticks = sw.ElapsedTicks;Let us see an example −Example Live Demousing System; using System.Linq; using System.Diagnostics; public class Demo { public static void Main() { var sw = Stopwatch.StartNew(); long ticks = sw.ElapsedTicks; Console.WriteLine(ticks); } }Output582
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
2K+ Views
You can format a TimeSpan in the hh: mm: ss format in C#.Firstly, set the TimeSpan −TimeSpan ts = new TimeSpan(9, 15, 30);To format TimeSpan −{0:hh\:mm\:ss}The following is the code −Example Live Demousing System; using System.Linq; public class Demo { public static void Main() { TimeSpan ts = new TimeSpan(9, 15, 30); Console.WriteLine("{0:hh\:mm\:ss}", ts); } }Output09:15:30