Server Side Programming Articles - Page 2444 of 2646

Object Initializer in C#

karthikeya Boyini
Updated on 23-Jun-2020 07:06:25

196 Views

Initialize an object of a class with object initialize.Using it, you can assign values to the fields at the time of creating an object.We created Employee object and assigned values using curly bracket at the same time.Employee empDetails = new Employee() {    EID = 10,    EmpName = "Tim",    EmpDept = "Finance" }Now access the values of the Employee class. For example, for name of the employee.empDetails.EmpNameLet us see the complete code −Example Live Demousing System; public class Demo {    public static void Main() {       Employee empDetails = new Employee() {         ... Read More

General Date Long Time ("G") Format Specifier in C#

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

283 Views

The General Date Long Time standard format specifier is a combination of the short date ("d") and long time ("T") patterns, separated by a space.Set the date −DateTime dt = new DateTime(2018, 1, 3, 3, 45, 20);Now, use the ToString() method and DateTimeFormatInfo.dt.ToString("G", DateTimeFormatInfo.InvariantInfo)Example Live Demousing System; using System.Globalization; class Demo {    static void Main() {       DateTime dt = new DateTime(2018, 1, 3, 3, 45, 20);       Console.WriteLine(dt.ToString("G",       DateTimeFormatInfo.InvariantInfo));    } }Output01/03/2018 03:45:20

C# Value Tuple

Chandu yadav
Updated on 10-Apr-2020 09:12:04

122 Views

A value type representation of the C# Tuple is Value Type Tuple. It was introduced in C# 7.0.Note − Add System.ValueTuple package to run ValueTuple program.Let’s see how to add it −Go to your projectRight click on the project in the Solution ExplorerSelect “Manage NuGet Packages”You will reach the NuGet Package Manager.Now, click the Browse tab and find “ValueTuple”Finally, add System.ValueTuple packageExampleusing System; class Program {    static void Main() {       var val = (5, 50, 500, 5000);       Console.WriteLine("Add System.ValueTuple package to run this program!");       if (val.Item2 == 50) {          Console.WriteLine(val);       }    } }OutputThe following is the output.(5, 50, 500, 5000);

Return the total elements in a sequence as a 64-bit signed integer in C#

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

116 Views

Firstly, set a string array.string[] num = { "One", "Two", "Three", "Four", "Five"};Use the Linq LongCount method to get the count of elements.num.AsQueryable().LongCount();Here is the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       string[] num = { "One", "Two", "Three", "Four", "Five"};       long res = num.AsQueryable().LongCount();       Console.WriteLine("{0} elements", res);    } }Output5 elements

C# Program to get the smallest and largest element from a list

George John
Updated on 23-Jun-2020 07:09:34

10K+ Views

Set a list.List list = new List { 150, 300, 400, 350, 450, 550, 600 };To get the smallest element, use the Min() method.list.AsQueryable().Min();To get the largest element, use the Max() method.list.AsQueryable().Max();Let us see the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       List list = new List { 150, 300, 400, 350, 450, 550, 600 };       foreach(long ele in list){          Console.WriteLine(ele);       }       // getting largest element       long max_num = list.AsQueryable().Max(); ... Read More

C# Program to display the first element from an array

Ankith Reddy
Updated on 23-Jun-2020 06:51:22

2K+ Views

The following is our array −double[] myArr = {20.5, 35.6, 45.7, 55.6, 79.7};To get the first element, use the First() method.myArr.AsQueryable().First();Let us see the complete code −Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo {    static void Main() {       double[] myArr = {20.5, 35.6, 45.7, 55.6, 79.7};       double res = myArr.AsQueryable().First();       Console.WriteLine(res);    } }Output20.5

Convert Decimal to Int64 (long) in C#

Samual Sam
Updated on 23-Jun-2020 06:50:42

8K+ Views

Use the Convert.ToInt64() method to convert Decimal to Int64 (long) in C#.Let’s say we have a decimal variable.decimal d = 310.23m;Now to convert it to Int64, use the Convert.ToInt64() method.long res; res = Convert.ToInt64(d);Let us see another example −Example Live Demousing System; class Demo {    static void Main() {       decimal d = 190.66m;       long res;       res = Convert.ToInt64(d);       Console.WriteLine("Converted Decimal '{0}' to Int64 value {1}", d, res);    } }OutputConverted Decimal '190.66' to Int64 value 191

Return a C# tuple from a method

karthikeya Boyini
Updated on 23-Jun-2020 06:52:32

399 Views

Firstly, create a tuple as shown below that calls a method.var tuple = Show();The above statement calls the following method −static Tuple Show()Under the method, return the tuple as shown below −Example Live Demousing System; public class Demo {    public static void Main() {       var tuple = Show();       Console.WriteLine(tuple.Item1);       Console.WriteLine(tuple.Item2);       Console.WriteLine(tuple.Item3);       Console.WriteLine(tuple.Item4);       Console.WriteLine(tuple.Item5);    }    static Tuple Show() {       return Tuple.Create(3, 5, 7, 9, 11);    } }Output3 5 7 9 11

Set tuple as a method parameter in C#

Arjun Thakur
Updated on 23-Jun-2020 06:51:59

5K+ Views

Firstly, set a tuplevar tuple = Tuple.Create(100, 200, 300);Now, pass the tuple as a method parameter −Show(tuple);Here’s our method.static void Show(Tuple tuple)Now call the tuple values one by one as shown below −Example Live Demousing System; public class Program {    public static void Main() {       var tuple = Tuple.Create(100, 200, 300);       Show(tuple);    }    static void Show(Tuple tuple) {       Console.WriteLine(tuple.Item1);       Console.WriteLine(tuple.Item2);       Console.WriteLine(tuple.Item3);    } }Output100 200 300

Convert.ToDecimal Method in C#

Samual Sam
Updated on 23-Jun-2020 06:53:07

4K+ Views

Convert a specified value to a decimal number using the Convert.ToDecimal() method.We have a string here.string stringVal = "2,345.26";Now, let us use the Convert.ToDecimal() method to convert it to a decimal number.decimal decimalVal; decimalVal = System.Convert.ToDecimal(stringVal);Let us now see the complete example −Example Live Demousing System; public class Demo {    public static void Main() {       decimal decimalVal;       string stringVal = "2,345.26";       decimalVal = System.Convert.ToDecimal(stringVal);       System.Console.WriteLine("String converted to decimal = {0} ", decimalVal);    } }OutputString converted to decimal = 2345.26

Advertisements