Found 34494 Articles for Programming

TimeSpan.From methods in C# ()

Chandu yadav
Updated on 22-Jun-2020 14:12:03

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

C# Program to display the number of days in a month

Arjun Thakur
Updated on 22-Jun-2020 14:12:25

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

C# program to display the next day

Ankith Reddy
Updated on 22-Jun-2020 14:12:50

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

C# program to display the previous day

George John
Updated on 22-Jun-2020 14:13:12

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

C# program to Loop over a two dimensional array

karthikeya Boyini
Updated on 22-Jun-2020 14:13:58

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

Empty List in C#

Samual Sam
Updated on 22-Jun-2020 14:14:20

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

C# Program to Subtract Two TimeSpan

karthikeya Boyini
Updated on 22-Jun-2020 14:04:46

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

Null List in C#

Samual Sam
Updated on 22-Jun-2020 14:05:11

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

Remove duplicates from a List in C#

karthikeya Boyini
Updated on 22-Jun-2020 14:06:01

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

Get the range of elements in a C# list

Samual Sam
Updated on 22-Jun-2020 14:06:43

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

Advertisements