Found 35164 Articles for Programming

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

George John
Updated on 23-Jun-2020 07:37:32

42 Views

We have a list without any element.List val = new List { };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 Main() {       List val = new List{ };       float a = val.AsQueryable().FirstOrDefault();       Console.WriteLine("Default Value = "+a);       if (a == 0.0f) {          a = 0.1f;       }       Console.WriteLine("Default Float value updated = "+a);    } }OutputDefault Value = 0 Default Float value updated = 0.1

C# String.PadLeft Method

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

279 Views

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; class Demo {    static void Main() {       string myStr = "DemoOne";       // set padding at the beginning       Console.WriteLine(myStr.PadLeft(10));       myStr = "DemoTWO";       // set padding at the beginning       Console.Write(myStr.PadLeft(15));    } }OutputDemoOne DemoTWO

C# Console BufferHeight Property

Chandu yadav
Updated on 23-Jun-2020 07:38:38

80 Views

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) = 0

C# Program to get the absolute value of the time

karthikeya Boyini
Updated on 23-Jun-2020 07:26:37

717 Views

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() {       TimeSpan ts = new TimeSpan(-7, -50, -25);       TimeSpan duration = ts.Duration();       Console.WriteLine(duration);    } }Output07:50:25

C# TimeSpan Max value

Ankith Reddy
Updated on 23-Jun-2020 07:27:05

581 Views

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
Updated on 23-Jun-2020 07:27:41

320 Views

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
Updated on 23-Jun-2020 07:28:31

100 Views

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 units = prod.AsQueryable().TakeLast(3);Let us see the complete code.Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       int[] prod = { 110, 290, 340, 540, 456, 698, 765, 789};       // value of last three products       IEnumerable units = prod.AsQueryable().TakeLast(3);       foreach (int res in units) {          Console.WriteLine(res);       }    } }Output698 765 789

C# DateTime Max Value

Chandu yadav
Updated on 23-Jun-2020 07:29:21

4K+ Views

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 max = DateTime.MaxValue;       Console.WriteLine(max);    } }Output12/31/9999 11:59:59 PM

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

karthikeya Boyini
Updated on 23-Jun-2020 07:28:58

298 Views

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 {    static void Main() {       DateTime dt = new DateTime(2018, 10, 2, 7, 59, 20);       Console.WriteLine(dt.ToString("g", DateTimeFormatInfo.InvariantInfo));       Console.WriteLine(dt.ToString("g", CultureInfo.CreateSpecificCulture("en-us")));    } }Output10/02/2018 07:59 10/2/2018 7:59 AM

How to initialize an empty DateTime in C#

Samual Sam
Updated on 23-Jun-2020 07:30:24

2K+ Views

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() {       DateTime dt = DateTime.MinValue;       Console.WriteLine(dt);    } }Output1/1/0001 12:00:00 AM

Advertisements