Convert.ToSingle Method in C#

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

619 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

C# DateTime to add days to the current date

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

5K+ 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

C# Program to skip elements of an array from the end

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

734 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

145 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

C# Program to skip elements from a sequence as long as the specified condition is true

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

124 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

C# Exponential (“E”) Format Specifier

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

583 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

1K+ 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

C# int.Parse Method

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

9K+ 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

1K+ 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

C# OfType() Method

Arjun Thakur
Updated on 23-Jun-2020 07:12:57

330 Views

Filter a collection on the basis of each of its elements type.Let’s say you have the following list with integer and string elements −list.Add("Katie"); list.Add(100); list.Add(200);To filter collection and get only elements with string type.var myStr = from a in list.OfType() select a;Work the same for integer type.var myInt = from a in list.OfType() select a;The following is the complete code −Example Live Demousing System; using System.Linq; using System.Collections; public class Demo {    public static void Main() {       IList list = new ArrayList();       list.Add("Katie");       list.Add(100);       list.Add(200);     ... Read More

Advertisements