Full Date and Short Time Format Specifier in C#

Ankith Reddy
Updated on 23-Jun-2020 07:19:26

210 Views

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 toSingle Method in C#

Samual Sam
Updated on 23-Jun-2020 07:19:07

1K+ Views

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

Add Days to Current Date in C#

George John
Updated on 23-Jun-2020 07:18:39

7K+ Views

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

Skip Elements of an Array from the End in C#

karthikeya Boyini
Updated on 23-Jun-2020 07:18:00

1K+ Views

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

Implicit Conversion from 64-bit Signed Integer Long to Decimal in C#

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

285 Views

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

Skip Elements from a Sequence in C#

Chandu yadav
Updated on 23-Jun-2020 07:17:02

244 Views

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

Chash Exponential E Format Specifier

karthikeya Boyini
Updated on 23-Jun-2020 07:15:36

1K+ Views

The ("E") format specifier converts a number to a string of the following form −"-d.ddd…E+ddd"Or"-d.ddd…e+ddd"Above, "d" is a digit (0-9).Prefix the exponent with an "E" or an "e".Example Live Demousing System; using System.Globalization; class Demo {    static void Main() {       double d = 3452.7678;       Console.WriteLine(d.ToString("E", CultureInfo.InvariantCulture));       Console.WriteLine(d.ToString("E10", CultureInfo.InvariantCulture));       Console.WriteLine(d.ToString("e", CultureInfo.InvariantCulture));       Console.WriteLine(d.ToString("e10", CultureInfo.InvariantCulture));    } }Output3.452768E+003 3.4527678000E+003 3.452768e+003 3.4527678000e+003

C# Enum IsDefined Method

Chandu yadav
Updated on 23-Jun-2020 07:14:32

2K+ Views

The IsDefined method returns true if a given integral value, or its name as a string, is present in a specified enum.The following is our enum −enum Subjects { Maths, Science, English, Economics };The above is initialized by default i.e.Maths = 0, Science = 1, English = 2, Economics = 3Therefore, when we will find 3 using IsDefined(), then it will return True as shown below −Example Live Demousing System; public class Demo {    enum Subjects { Maths, Science, English, Economics };    public static void Main() {       object ob;       ob = 3;       Console.WriteLine("{0} = {1}", ob, Enum.IsDefined(typeof(Subjects), ob));    } }Output3 = True

Chash int Parse Method

Sai Subramanyam
Updated on 23-Jun-2020 07:14:09

23K+ Views

Convert a string representation of number to an integer, using the int.Parse method in C#. If the string cannot be converted, then the int.Parse method returns an exceptionLet’s say you have a string representation of a number.string myStr = "200";Now to convert it to an integer, use the int.Parse(). It will get converted.int.Parse(myStr);Example Live Demousing System.IO; using System; class Program {    static void Main() {       int res;       string myStr = "200";       res = int.Parse(myStr);       Console.WriteLine("String is a numeric representation: "+res);    } }OutputString is a numeric representation: 200

C# LINQ Where Method

karthikeya Boyini
Updated on 23-Jun-2020 07:13:44

3K+ Views

The Where method filters an array of values based on a predicate.Here, the predicate is checking for elements above 70.Where((n, index) => n >= 70);Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       int[] arr = { 10, 30, 20, 15, 90, 85, 40, 75 };       Console.WriteLine("Array:");       foreach (int a in arr)       Console.WriteLine(a);       // getting elements above 70       IEnumerable myQuery = arr.AsQueryable().Where((n, index) => n >= 70);       Console.WriteLine("Elements above 70...:");       foreach (int res in myQuery)       Console.WriteLine(res);    } }OutputArray: 10 30 20 15 90 85 40 75 Elements above 70...: 90 85 75

Advertisements