- 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
104 Views
The TimeSpan.From methods include FromDays, FromHours, FromMinutes, etc.To get the TimeSpan for all the methods// Days TimeSpan t1 = TimeSpan.FromDays(1); // Hours TimeSpan t2 = TimeSpan.FromHours(1); // Minutes TimeSpan t3 = TimeSpan.FromMinutes(1);The following is the code that works on all the TimeSpan methods −Example Live Demousing System; using System.Linq; public class Demo { public static void Main() { TimeSpan t1 = TimeSpan.FromDays(1); TimeSpan t2 = TimeSpan.FromHours(1); TimeSpan t3 = TimeSpan.FromMinutes(1); Console.WriteLine(t1); Console.WriteLine(t2); Console.WriteLine(t3); } }Output1.00:00:00 01:00:00 00:01:00
597 Views
Use the DaysInMonth to display the number of days in a month.Add the year and month as a parameter for the DaysInMonth() method −DateTime.DaysInMonth(2018, 8);The following is an example −Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { Console.WriteLine("Today = {0}", DateTime.Today); int num_days = DateTime.DaysInMonth(2018, 8); Console.WriteLine("Days in August: "+num_days); } }OutputToday = 9/4/2018 12:00:00 AM Days in August: 31
2K+ Views
To display the next day, use the AddDays() method and a value +1 to get the next day.Firstly, use the following to get the current day −DateTime.TodayNow, add 1 to the AddDays() method to get the next day −DateTime.Today.AddDays(1)The following is the code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { Console.WriteLine("Today = {0}", DateTime.Today); Console.WriteLine("Previous Day = {0}", DateTime.Today.AddDays(-1)); Console.WriteLine("Next Day (Tomorrow) = {0}", DateTime.Today.AddDays(1)); } }OutputToday = 9/4/2018 12:00:00 AM Previous Day = 9/3/2018 12:00:00 AM Next Day (Tomorrow) = 9/5/2018 12:00:00 AM
3K+ Views
To display the previous day, use the AddDays() method and a value -1 to get the previous day.Firstly, use the following to get the current day −DateTime.TodayNow, add -1 to the AddDays() method to get the previous day −DateTime.Today.AddDays(-1)The following is the code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { Console.WriteLine("Today = {0}", DateTime.Today); Console.WriteLine("Previous Day = {0}", DateTime.Today.AddDays(-1)); } }OutputToday = 9/4/2018 12:00:00 AM Previous Day = 9/3/2018 12:00:00 AM
4K+ Views
Declare a two dimensional array −string[, ] array = new string[3, 3];Set elements in the array −array[0, 0] = "One"; array[0, 1] = "Two"; array[0, 2] = "Three"; array[1, 0] = "Four"; array[1, 1] = "Five"; array[1, 2] = "Six"; array[2, 0] = "Seven"; array[2, 1] = "Eight"; array[2, 2] = "Nine";Now, get the upper bound to get the dimensions to loop over the array −int uBound0 = array.GetUpperBound(0); int uBound1 = array.GetUpperBound(1);Iterate through a nested loop, until the above two values as shown in the code below −Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Demo { ... Read More
8K+ Views
Set a list that has zero elements −List myList = new List();Now check whether the list is empty or null −Console.WriteLine(myList == null);Above, returns “False” i.e. the list is not null - the list is empty.Let us see the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { List myList = new List(); // returns false i.e. an empty list (not a null list) Console.WriteLine(myList == null); } }OutputFalse
350 Views
Firstly, set two TimeSpans −TimeSpan t1 = TimeSpan.FromMinutes(2); TimeSpan t2 = TimeSpan.FromMinutes(1);To add it, use the Subtract() method −imeSpan res = t1.Subtract(t2);Here is the complete code −Example Live Demousing System; using System.Linq; public class Demo { public static void Main() { TimeSpan t1 = TimeSpan.FromMinutes(2); TimeSpan t2 = TimeSpan.FromMinutes(1); Console.WriteLine("First TimeSpan: "+t1); Console.WriteLine("Second TimeSpan: "+t2); // Subtracting TimeSpan res = t1.Subtract(t2); Console.WriteLine("Resultant TimeSpan: "+res); } }OutputFirst TimeSpan: 00:02:00 Second TimeSpan: 00:01:00 Resultant TimeSpan: 00:01:00
2K+ Views
A null list exists in C#. To check whether a list is null or not, check them against the null literal. Set a null like this −List myList = null;Now, to check for null, use the equality operator −myList == null;The following is an example −Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { List myList = null; // checking for null Console.WriteLine(myList == null); } }OutputTrue
6K+ Views
Use the Distinct() method to remove duplicates from a list in C#.Firstly, add a new list −List arr1 = new List(); arr1.Add(10); arr1.Add(20); arr1.Add(30); arr1.Add(40); arr1.Add(50); arr1.Add(30); arr1.Add(40); arr1.Add(50);To remove duplicate elements, use the Distinct() method as shown below −List distinct = arr1.Distinct().ToList();Here is the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { List arr1 = new List(); arr1.Add(10); arr1.Add(20); arr1.Add(30); arr1.Add(40); arr1.Add(50); arr1.Add(30); arr1.Add(40); ... Read More
2K+ Views
Use the GetRange() method to get the range of elements −Firstly, set a list and add elements −List arr1 = new List(); arr1.Add(10); arr1.Add(20); arr1.Add(30); arr1.Add(40); arr1.Add(50);Now, under a new list get the range of elements between index 1 and 3 −List myList = arr1.GetRange(1, 3);Here is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Demo { public static void Main() { List arr1 = new List(); arr1.Add(10); arr1.Add(20); arr1.Add(30); arr1.Add(40); arr1.Add(50); Console.WriteLine("Initial List ..."); ... Read More