Convert ChangeType Method in C#

Chandu yadav
Updated on 23-Jun-2020 09:05:00

3K+ Views

The ChangeType() method returns an object of the specified type and whose value is equivalent to the specified object.Let’s say we have a double type.double val = -3.456Now, use the ChangeType method to change the type to integer.num = (int)Convert.ChangeType(val, TypeCode.Int32);Let us see the complete example.Example Live Demousing System; public class Demo {    public static void Main() {       double val = -3.456;       int num = (int)Convert.ChangeType(val, TypeCode.Int32);       Console.WriteLine("{0} converted to an Int32: {1}", val, num);    } }Output-3.456 converted to an Int32: -3

Convert toByte Method in C#

Arjun Thakur
Updated on 23-Jun-2020 09:04:35

1K+ Views

The Convert.ToByte method is used to convert a specified value to an 8-bit unsigned integer.Let’s say we have a char variable.Char charVal = ‘a’;Now, convert it to an 8-bit unsigned integer.byte byteVal = Convert.ToByte(charVal);Let us see another example now.Example Live Demousing System; public class Demo {    public static void Main() {       char[] charVal = { 'p', 'q', 'r', 's' };       foreach (char c in charVal) {          byte byteVal = Convert.ToByte(c);          Console.WriteLine("{0} is converted to = {1}", c, byteVal);       }    } }Outputp is converted to = 112 q is converted to = 113 r is converted to = 114 s is converted to = 115

Convert ToBoolean Method in C#

Samual Sam
Updated on 23-Jun-2020 09:04:09

2K+ Views

The Convert.ToBoolean method is used to convert a specified value to an equivalent Boolean value.The following is our double type.double doubleNum = 329.34;To convert it to Boolean, use the Convert.ToBoolean() method.bool boolNum; boolNum = System.Convert.ToBoolean(doubleNum);Let us see another example.Example Live Demousing System; public class Demo {    public static void Main() {       double doubleNum = 3.4;       bool boolNum;       // Double to bool       boolNum = System.Convert.ToBoolean(doubleNum);       System.Console.WriteLine("{0} as a Boolean = {1}.", doubleNum, boolNum);    } }Output3.4 as a Boolean = True.

C# LINQ Last Method

karthikeya Boyini
Updated on 23-Jun-2020 09:03:47

1K+ Views

Get the last element from a sequence using the Linq Last() method.The following is our array.int[] val = { 10, 20, 30, 40 };Now, get the last element.val.AsQueryable().Last();Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       int[] val = { 10, 20, 30, 40 };       // getting last element       int last_num = val.AsQueryable().Last();       Console.WriteLine("Last element: "+last_num);    } }OutputLast element: 40

Convert ToUInt64 Method in C#

Ankith Reddy
Updated on 23-Jun-2020 09:03:26

186 Views

Use the Convert.ToUInt64() method to convert a specified value to a 64-bit unsigned integer.The following is our char.char ch = 'a';Now, let’s convert it to a 64-bit unsigned integer.ulong res; res = Convert.ToUInt64(ch);Here is the complete example.Example Live Demousing System; public class Demo {    public static void Main() {       char ch = 'a';       ulong res;       res = Convert.ToUInt64(ch);       Console.WriteLine("Converted char value '{0}' to {1}", ch, res);    } }OutputConverted char value 'a' to 97

The 0 Custom Format Specifier in C#

karthikeya Boyini
Updated on 23-Jun-2020 09:02:56

2K+ Views

The “0” custom specifier is a zero placeholder.If the value to be formatted has a digit in the position where the zero appears in the format string, the the digit is copied to the resultant string. However, if this doesn’t happen, then a zero appears.Here is our double variable.double d; d = 292;Now, by setting the following, you can easily make zero appear in the format string.d.ToString("00000")Example Live Demousing System; using System.Globalization; class Demo {    static void Main() {       double d;       d = 292;       Console.WriteLine(d.ToString("00000"));       Console.WriteLine(String.Format("{0:00000}", d));   ... Read More

Custom Specifier in C#

Samual Sam
Updated on 23-Jun-2020 09:02:33

252 Views

The "." custom format specifier adds a localized decimal separator into the output string.The 1st period in the format string determines the location of the decimal separator in the formatted value.double d = 2.3; d.ToString("0.00", CultureInfo.InvariantCultureLet us see another example to learn how to implement “.” Custom specifier.Example Live Demousing System; using System.Globalization; class Demo {    static void Main() {       double d;       d = 3.7;       Console.WriteLine(d.ToString("0.00", CultureInfo.InvariantCulture));       Console.WriteLine(String.Format(CultureInfo.InvariantCulture, "{0:0.00}", d));       d = 5.89;       Console.WriteLine(d.ToString("00.00", CultureInfo.InvariantCulture));       Console.WriteLine(String.Format(CultureInfo.InvariantCulture, "{0:00.00}", d));    } }Output3.70 3.70 05.89 05.89

C# Program to Find a Key in Dictionary

Ankith Reddy
Updated on 23-Jun-2020 09:02:10

331 Views

Firstly, set a Dictionary collection with elements.Dictionary d = new Dictionary() {    {1, "Applianes"},    {2, "Clothing"},    {3, "Toys"},    {4, "Footwear"},    {5, "Accessories"} };Now, let’s say you need to check whether key 5 exists or not. For that, use ContainsKey() method. It returns True if key is found.d.ContainsKey(5);Let us see the complete code.Example Live Demousing System; using System.Collections.Generic; public class Program {    public static void Main() {       Dictionary d = new Dictionary() {          {1, "Electronics"},          {2, "Clothing"},          {3, "Toys"},     ... Read More

Binary Search Method in C#

karthikeya Boyini
Updated on 23-Jun-2020 09:01:26

546 Views

BinarySearch() works on a sorted list whether its numeric, alphanumeric or strings. It finds you the index of an element.Let’s say the following is our list.List list = new List(); list.Add(70); list.Add(150); list.Add(220); list.Add(250); list.Add(300);Now to check the index where 250 is placed, use BinarySearch() method.list.BinarySearch(250);Example Live Demousing System; using System.Collections.Generic; class Demo {    static void Main() {       List list = new List();       list.Add(70);       list.Add(150);       list.Add(220);       list.Add(250);       list.Add(300);       int value = list.BinarySearch(250);       Console.WriteLine("Element 250 at Index: "+value);    } }OutputElement 250 at Index: 3

Size of a Three-Dimensional Array in C#

George John
Updated on 23-Jun-2020 09:01:00

1K+ Views

To get the size of a 3D array in C#, use the GetLength() method with parameter as the index of the dimensions.GetLength(dimensionIndex)To get the size.arr.GetLength(0) arr.GetLength(1) arr.GetLength(2)Example Live Demousing System; class Program {    static void Main() {       int[,,] arr = new int[3,4,5];       // length of a dimension       Console.WriteLine(arr.GetLength(0));       Console.WriteLine(arr.GetLength(1));       Console.WriteLine(arr.GetLength(2));       // length       Console.WriteLine(arr.Length);    } }Output3 4 5 60

Advertisements