- 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
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
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
This method returns true if the entered string has only whitespace characters or is null.Let’s say you are checking for whitespace character.bool val1 = string.IsNullOrWhiteSpace(“ “);The above returns True, since the string is a whitespace character. In the same way, check it for null using the IsNullOrWhiteSpace() method.Here is the entire example that checks for null and whitespace −Exampleusing System; using System.IO; public class Demo { public static void Main() { bool val1 = string.IsNullOrWhiteSpace(“100”); bool val2 = string.IsNullOrWhiteSpace(" "); bool val3 = string.IsNullOrWhiteSpace(null); Console.WriteLine(val1); Console.WriteLine(val2); Console.WriteLine(val3); } }
The Long Time format specifier represents a custom date and time format string.It is defined by DateTimeFormatInfo.LongTimePattern property.The custom format string.HH:mm:ssExample Live Demousing System; using System.Globalization; class Demo { static void Main() { DateTime date = new DateTime(2018, 9, 9, 8, 15, 30); Console.WriteLine(date.ToString("T", CultureInfo.CreateSpecificCulture("en-us"))); } }Output8:15:30 AM
The Full Date Short Time standard format specifier represents a combination of the long date ("D") and short time ("t") patterns.UseDateTime to set the date.DateTime myDate = new DateTime(2018, 8, 29, 1, 10, 0);Now, use the (“f”) format specifier like this −myDate.ToString("f", CultureInfo.CreateSpecificCulture("en-US"))The following is an example −Example Live Demousing System; using System.Globalization; class Demo { static void Main() { DateTime myDate = new DateTime(2018, 8, 29, 1, 10, 0); Console.WriteLine(myDate.ToString("f",CultureInfo.CreateSpecificCulture("en-US"))); } }OutputWednesday, August 29, 2018 1:10 AM
Convert a specified value to a single-precision floating-point number using the Convert.ToSingle() method in C#.Here is our bool −bool boolVal = false;Now, let us use the ToSingle() method to convert the value to a single precision floating-point.float floatVal; floatVal = Convert.ToSingle(boolVal);Example Live Demousing System; public class Demo { public static void Main() { bool boolVal = false; float floatVal; floatVal = Convert.ToSingle(boolVal); Console.WriteLine("Converted {0} to {1}", boolVal, floatVal); } }OutputConverted False to 0
Firstly, get the current date.DateTime.TodayNow, use AddDays() method to add days to the current date. Here, we are adding 10 days to the current date.DateTime.Today.AddDays(10)Let us see the complete code −Example Live Demousing System; using System.Linq; public class Demo { public static void Main() { Console.WriteLine("Today = {0}", DateTime.Today); Console.WriteLine("Add 10 Days = {0}", DateTime.Today.AddDays(10)); } }OutputToday = 9/11/2018 12:00:00 AM Add 10 Days = 9/21/2018 12:00:00 AM
Declare an array and initialize elements.int[] marks = { 45, 50, 60, 70, 85 };Use the SkipLast() method to skip elements of an array from the end.IEnumerable selMarks = marks.AsQueryable().SkipLast(3);The elements are skipped and rest of the elements is returned as shown below −Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { int[] marks = { 45, 50, 60, 70, 85 }; Console.WriteLine("Array..."); foreach (int res in marks) Console.WriteLine(res); IEnumerable selMarks = marks.AsQueryable().SkipLast(3); Console.WriteLine("Array after skipping last 3 elements..."); foreach (int res in selMarks) Console.WriteLine(res); } }OutputArray... 45 50 60 70 85 Array after skipping last 3 elements... 45 50
The long type represents a 64-bit signed integer.To implicitly convert a 64-bit signed integer to a Decimal, firstly set a long value.long val = 989678876876876;To convert long to decimal, assign the value.dec = val;Let us see another example −Example Live Demousing System; public class Demo { public static void Main() { long val = 76755565656565; decimal dec; Console.WriteLine("Implicit conversion from 64-bit signed integer (long) to Decimal"); dec = val; Console.WriteLine("Decimal : "+dec); } }OutputImplicit conversion from 64-bit signed integer (long) to Decimal Decimal : 76755565656565
Use the SkipWhile() method to skip elements from a sequence as long as the specified condition is true.The following is the array −int[] marks = { 35, 42, 48, 88, 55, 90, 95, 85 };Here is the condition.s => s >= 50As long as the above condition is true, the elements above 50 are skipped as shown below −Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { int[] marks = { 35, 42, 48, 88, 55, 90, 95, 85 }; // skips elements above 50 IEnumerable selMarks = marks.AsQueryable().OrderByDescending(s => s).SkipWhile(s => s >= 50); // displays rest of the elements Console.WriteLine("Skipped marks > 60..."); foreach (int res in selMarks) { Console.WriteLine(res); } } }OutputSkipped marks > 60... 48 42 35