Display the default if element is not found in a C# List

George John
Published on 12-Sep-2018 12:27:15
We have a list without any element.List<float> val = new List<float> { };To display the default and avoid any error, use the FirstorDefault() method.val.AsQueryable().FirstOrDefault();You can also change the value to be displayed as default.Let us see the code.Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Demo {    static void ... Read More

C# String.PadLeft Method

Samual Sam
Published on 12-Sep-2018 12:27:05
Pad the beginning of the string with spaces using the PadLeft() method. You can also pad it with a Unicode character.Let’s say the following is our string.string myStr = "DemoOne";To set a padding at the beginning of the above string, use the PadLeft method.myStr.PadLeft(10);Here is the complete example.Example Live Demousing System; ... Read More

C# Console BufferHeight Property

Chandu yadav
Published on 12-Sep-2018 12:21:08
Use the BufferHeight gets or sets the height of the buffer area.Use the property like this −Console.BufferHeightLet us see the complete example.Example Live Demousing System; class Demo {    static void Main() {       Console.WriteLine("Buffer height (rows) = "+Console.BufferHeight);    } }OutputBuffer height (rows) = 0Read More

C# Program to get the absolute value of the time

karthikeya Boyini
Published on 12-Sep-2018 12:20:16
To get the absolute value of time, use the TimesSpan Duration() method.Let’s say the following is our TimeSpan.TimeSpan ts = new TimeSpan(-7, -50, -25);Now to get the absolute value.TimeSpan duration = ts.Duration();Let us see the complete code.Example Live Demousing System; using System.Linq; public class Demo {    public static void Main() ... Read More

C# TimeSpan Max value

Ankith Reddy
Published on 12-Sep-2018 12:17:52
Timespan shows the length of time.To get the maximum value of TimeSpan, use the following property.TimeSpan.MaxValueExample Live Demousing System; using System.Linq; public class Demo {    public static void Main() {       Console.WriteLine(TimeSpan.MaxValue);    } }Output10675199.02:48:05.4775807

C# TimeSpan Min value

Samual Sam
Published on 12-Sep-2018 12:17:02
Timespan shows the length of time.To get the minimum value of TimeSpan, use the following property.TimeSpan.MinValueExample Live Demousing System; using System.Linq; public class Demo {    public static void Main() {       Console.WriteLine(TimeSpan.MinValue);    } }Output-10675199.02:48:05.4775808

C# Program to return specified number of elements from the end of an array

Arjun Thakur
Published on 12-Sep-2018 12:13:37
Use the TakeLast() method to return elements from the end of an array.Let us first declare and initialize an array.int[] prod = { 110, 290, 340, 540, 456, 698, 765, 789};Now, let’s get the last three elements.IEnumerable<int> units = prod.AsQueryable().TakeLast(3);Let us see the complete code.Example Live Demousing System; using System.Linq; using ... Read More

C# General Date Short Time ("g") Format Specifier

karthikeya Boyini
Published on 12-Sep-2018 12:13:15
The Generate Date Short Time format specifier is a combination of the short date ("d") and short time ("t") patterns, separated by a space.Set a date using DateTime.DateTime dt = new DateTime(2018, 10, 2, 7, 59, 20);Now, use the (“g”) format specifier.dt.ToString("g", DateTimeFormatInfo.InvariantInfo));Example Live Demousing System; using System.Globalization; class Demo { ... Read More

C# DateTime Max Value

Chandu yadav
Published on 12-Sep-2018 12:11:29
To set the max value for a Date, use the DateTime property MaxValue.DateTime max = DateTime.MaxValue;Now, display the value of max to get the maximum value of a date as shown below.Example Live Demousing System; using System.Linq; public class Demo {    public static void Main() {       DateTime ... Read More

How to initialize an empty DateTime in C#

Samual Sam
Published on 12-Sep-2018 12:07:22
Set a DateTime to its minimum value.DateTime.MinValue;The above will display the minimum value i.e.1/1/0001Let us see how to display the minimum value and avoid adding null to a date to initialize it as empty.Example Live Demousing System; using System.Linq; public class Demo {    public static void Main() {     ... Read More
Advertisements