Found 35164 Articles for Programming

Generate current month in C#

George John
Updated on 23-Jun-2020 07:29:56

9K+ Views

To display current month, firstly use “Now“ to get the current date.DateTime dt = DateTime.Now;Now, use the Month property to get the current month.dt.MonthLet us see the complete code.Example Live Demousing System; using System.Linq; public class Demo {    public static void Main() {       DateTime dt = DateTime.Now;       Console.WriteLine(dt.Month);    } }Output9To display current month’s name.Example Live Demousing System; using System.Linq; public class Demo {    public static void Main() {       DateTime dt = DateTime.Now;       Console.WriteLine(dt.ToString("MMM"));    } }OutputSep

C# Program to get current day of week

Ankith Reddy
Updated on 23-Jun-2020 07:31:14

12K+ Views

Use DateTime. DayOfWeek property to display the current day of week.DayOfWeek wk = DateTime.Today.DayOfWeek;Now, displaying “wk” would give you the current day of the week.Let us see the complete code to get the current day of week.Example Live Demousing System; using System.Linq; public class Demo {    public static void Main() {       DayOfWeek wk = DateTime.Today.DayOfWeek;       Console.WriteLine(wk);    } }OutputWednesday

C# Program to check whether the elements of a sequence satisfy a condition or not

karthikeya Boyini
Updated on 23-Jun-2020 07:30:47

228 Views

Use All() Method to check whether the elements of a sequence satisfy a condition or not. Even if one of the element do not satisfy the set condition, the All() method returns False.To set conditions, use Lambda Expressions. Below shows a condition to check whether all the elements are greater than 20 or not.myArr.AsQueryable().All(val => val > 20);Let us see an example.Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       int[] myArr = {7, 15, 22, 30, 40};       // checking if all the array elements are greater than 20       bool res = myArr.AsQueryable().All(val => val > 20);       Console.WriteLine(res);    } }OutputFalse

C# Program to cast a type to its IEnumerable equivalent

Arjun Thakur
Updated on 23-Jun-2020 07:21:04

330 Views

Use the AsEnumerable() method to cast a type to its IEnumerable equivalent. It is an extension method.For our example, we have set an array.int[] myArr = new int[10]; myArr[0] = 1; myArr[1] = 2; myArr[2] = 3; myArr[3] = 4; myArr[4] = 5;Now, we have used the AsEnumerable() method to cast.myArr.AsEnumerable();Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       int[] myArr = new int[10];       myArr[0] = 1;       myArr[1] = 2;       myArr[2] = 3;       myArr[3] = 4;       myArr[4] = 5;       myArr[5] = 6;       myArr[6] = 7;       myArr[7] = 8;       myArr[8] = 9;       myArr[9] = 10;       // AsEnumerable       var a = myArr.AsEnumerable();       // Displaying       foreach (var item in a) {          Console.WriteLine(item);       }    } }Output1 2 3 4 5 6 7 8 9 10

C# difference in milliseconds between two DateTime

Samual Sam
Updated on 23-Jun-2020 07:21:31

6K+ Views

Let’s say the following are two DateTime objects for our dates.DateTime date1 = new DateTime(2018, 8, 11, 08, 15, 20); DateTime date2 = new DateTime(2018, 8, 11, 11, 14, 25);Find the difference between both these dates using TimeSpan.TimeSpan ts = date2 - date1;Now to get the Milliseconds, use the following property −ts.TotalMillisecondsLet us see the complete code.Example Live Demousing System; using System.Linq; public class Demo {    public static void Main() {       DateTime date1 = new DateTime(2018, 8, 11, 08, 15, 20);       DateTime date2 = new DateTime(2018, 8, 11, 11, 14, 25);       TimeSpan ts = date2 - date1;       Console.WriteLine("No. of Seconds (Difference) = {0}", ts.TotalMilliseconds);    } }OutputNo. of Seconds (Difference) = 10745000

C# Console.WindowLeft Property

Ankith Reddy
Updated on 23-Jun-2020 07:21:53

156 Views

The WindowsLeft property gets or sets the leftmost position of the console window area relative to the screen buffer.Declare an integer variable to get the leftmost position.int left;Now, use the Console.WindowLeft property.left = Console.WindowLeftLet us see the complete example.Example Live Demousing System; class Demo {    static void Main() {       int left;       left = Console.WindowLeft;       Console.WriteLine("Left position of the Console window = "+left);    } }OutputNote: The output may vary accordingly based on the position of the Console WindowLeft position of the Console window = 0

Calculate minutes between two dates in C#

George John
Updated on 23-Jun-2020 07:22:38

16K+ Views

Firstly, set the two dates.DateTime date1 = new DateTime(2018, 7, 15, 08, 15, 20); DateTime date2 = new DateTime(2018, 8, 17, 11, 14, 25);Now, calculate the difference between two dates.TimeSpan ts = date2 - date1;To calculate minutes.ts.TotalMinutesLet us see the complete code.Example Live Demousing System; using System.Linq; public class Demo {    public static void Main() {       DateTime date1 = new DateTime(2018, 7, 15, 08, 15, 20);       DateTime date2 = new DateTime(2018, 8, 17, 11, 14, 25);       TimeSpan ts = date2 - date1;       Console.WriteLine("No. of Minutes (Difference) = {0}", ts.TotalMinutes);    } }OutputNo. of Minutes (Difference) = 47699.0833333333

C# Program to get the difference between two dates in seconds

karthikeya Boyini
Updated on 23-Jun-2020 07:22:16

5K+ Views

Set two dates.DateTime date1 = new DateTime(2018, 7, 15, 08, 15, 20); DateTime date2 = new DateTime(2018, 7, 15, 11, 14, 25);Now calculate the difference between two dates.TimeSpan ts = date2 - date1;Move further and calculate the difference in seconds.ts.TotalSecondsLet us see the complete code.Example Live Demousing System; using System.Linq; public class Demo {    public static void Main() {       DateTime date1 = new DateTime(2018, 7, 15, 08, 15, 20);       DateTime date2 = new DateTime(2018, 7, 15, 11, 14, 25);       TimeSpan ts = date2 - date1;       Console.WriteLine("No. of Seconds (Difference) = {0}", ts.TotalSeconds);    } }OutputNo. of Seconds (Difference) = 10745

C# Console.WindowHeight Property

Samual Sam
Updated on 23-Jun-2020 07:22:58

146 Views

The WindowHeight property gets or sets the height of the console window.Declare a variable.int height;Now, get the height of the current window.height = Console.WindowHeight;The following is the complete example −Example Live Demousing System; using System.Numerics; using System.Globalization; class Demo {    static void Main() {       int height;       height = Console.WindowHeight;       Console.WriteLine("Current window height = "+height);    } }OutputCurrent window height = 0

C# Program to add a node after the given node in a Linked List

Chandu yadav
Updated on 23-Jun-2020 07:24:57

191 Views

Set a LinkedList and add elements.string [] students = {"Beth", "Jennifer", "Amy", "Vera"}; LinkedList list = new LinkedList(students);Firstly, add a new node at the end.var newNode = list.AddLast("Emma");Now, use the AddAfter() method to add a node after the given node.list.AddAfter(newNode, "Matt");The following is the complete code.Example Live Demousing System; using System.Collections.Generic; class Demo {    static void Main() {       string [] students = {"Beth", "Jennifer", "Amy", "Vera"};       LinkedList list = new LinkedList(students);       foreach (var stu in list) {          Console.WriteLine(stu);       }       // ... Read More

Advertisements