Convert a specified value to a 16-bit signed integer using the Convert.ToInt16 method in C#.We have a double variable with a value initialized to it.double doubleNum = 3.456;Now, let us convert it to Int16 i.e. short.short shortNum; shortNum = Convert.ToInt16(doubleNum);Here is the complete example −Example Live Demousing System; public class Demo { public static void Main() { double doubleNum = 3.456; short shortNum; shortNum = Convert.ToInt16(doubleNum); Console.WriteLine("Converted {0} to {1}", doubleNum, shortNum); } }OutputConverted 3.456 to 3
Use the Convert.ToUInt16 method to convert a specified value to a 16-bit unsigned integer.Here is our string −string str = "1129";Now let us convert it to 16-bit unsigned integer.ushort res; res = Convert.ToUInt16(str);The following is the complete example −Example Live Demousing System; public class Demo { public static void Main() { string str = "1307"; ushort res; res = Convert.ToUInt16(str); Console.WriteLine("Converted string '{0}' to {1}", str, res); } }OutputConverted string '1307' to 1307
Enumerable.Repeat method is part of System.Linq namespace.It returns a collection with repeated elements in C#.Firstly, set which element you want to repeat and how many times.As an example, let us see how to repeat number 10, five times −Enumerable.Repeat(10, 5);The following is the complete example −Example Live Demousing System; using System.Linq; class Demo { static void Main() { var val = Enumerable.Repeat(10, 5); foreach (int res in val) { Console.WriteLine(res); } } }Output10 10 10 10 10
The Aggregate() method applies an accumulator function over a sequence.The following is our array −string[] arr = { "DemoOne", "DemoTwo", "DemoThree", "DemoFour"};Now use the Aggregate() method. We have set the ssed value as “DemoFive” for comparison.string res = arr.AsQueryable().Aggregate("DemoFive", (longest, next) => next.Length > longest.Length ? next : longest, str => str.ToLower());Here, the resultant string should have more number of characters than the initial seed value i.e. “DemoFive”.Example Live Demousing System; using System.Linq; class Demo { static void Main() { string[] arr = { "DemoOne", "DemoTwo", "DemoThree", "DemoFour"}; string res = arr.AsQueryable().Aggregate("DemoFive", (longest, next) ... Read More
To represent Int64 as a Binary string in C#, use the ToString() method and set the base as the ToString() method’s second parameter i.e. 2 for Binary.Int64 represents a 64-bit signed integer.Firstly, set an Int64 variable.long val = 753458;Now, convert it to binary string by including 2 as the second parameter.Convert.ToString(val, 2)Example Live Demousing System; class Demo { static void Main() { long val = 753458; Console.WriteLine("Long: "+val); Console.Write("Binary String: "+Convert.ToString(val, 2)); } }OutputLong: 753458 Binary String: 10110111111100110010
The All Method checks for all the values in a collection and returns a Boolean. Even if one of the element do not satisfy the set condition, the All() method returns False.Let us see an example −int[] arr = {10, 15, 20};Now, using All() method, we will check whether each element in the above array is greater than 5 or not.arr.AsQueryable().All(val => val > 5);Example Live Demousing System; using System.Linq; class Demo { static void Main() { int[] arr = {10, 15, 20}; // checking if all the array elements are greater than 5 bool res = arr.AsQueryable().All(val => val > 5); Console.WriteLine(res); } }OutputTrue
Here is the string.string str = "ppqqrr";Now, use Hashset to map the string to char. This will remove the duplicate characters from a string.var res = new HashSet(str);Let us see the complete example −Example Live Demousing System; using System.Linq; using System.Collections.Generic; namespace Demo { class Program { static void Main(string[] args) { string str = "ppqqrr"; Console.WriteLine("Initial String: "+str); var res = new HashSet(str); Console.Write("New String after removing duplicates:"); foreach (char c in res){ Console.Write(c); } } } }OutputInitial String: ppqqrr New String after removing duplicates:pqr
To get the distinct elements, use the Distinct() method.The following is our list with duplicate elements.List points = new List { 5, 10, 5, 20, 30, 30, 40, 50, 60, 70 };Now to get the distinct elements −points.AsQueryable().Distinct();Let us see the entire example −Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo { static void Main() { List points = new List { 5, 10, 5, 20, 30, 30, 40, 50, 60, 70 }; // distict elements from the list IEnumerable res = points.AsQueryable().Distinct(); foreach (int a in res) { Console.WriteLine(a); } } }Output5 10 20 30 40 50 60 70
This round-trip ("R") format specifier is supported for the Single, Double, and BigInteger types.It ensures that a numeric value converted to a string is parsed back into the same numeric value.Let us see an example −Firstly, we have a double variable.double doubleVal = 0.91234582637;Now, use the ToString() method: and set the Round-trip format specifier.doubleVal.ToString("R", CultureInfo.InvariantCulture);Let us see the complete example −Example Live Demousing System; using System.Numerics; using System.Globalization; class Demo { static void Main() { double doubleVal = 0.91234582637; string str = doubleVal.ToString("R", CultureInfo.InvariantCulture); double resRound = double.Parse(str, CultureInfo.InvariantCulture); ... Read More
The hexadecimal ("X") format specifier is used to convert a number to a string of hexadecimal digits.Set the case of the format specifier for uppercase or lowercase characters to be worked on hexadecimal digits greater than 9.Let us understand this with an example −“X” for PQR, whereas “x” for pqrExample Live Demousing System; using System.Numerics; using System.Globalization; class Demo { static void Main() { int num; num = 345672832; Console.WriteLine(num.ToString("X")); Console.WriteLine(num.ToString("X2")); num = 0x307e; Console.WriteLine(num.ToString("x")); Console.WriteLine(num.ToString("X")); } }Output149A8C80 149A8C80 307e 307E
 
 Data Structure
 Data Structure Networking
 Networking RDBMS
 RDBMS Operating System
 Operating System Java
 Java MS Excel
 MS Excel iOS
 iOS HTML
 HTML CSS
 CSS Android
 Android Python
 Python C Programming
 C Programming C++
 C++ C#
 C# MongoDB
 MongoDB MySQL
 MySQL Javascript
 Javascript PHP
 PHP 
		 
		 
		 
		 
		