Found 2587 Articles for Csharp

C# Decimal ("D") Format Specifier

George John
Updated on 23-Jun-2020 08:55:14

2K+ Views

The "D" (or decimal) format specifier works for integer type. It converts a number to a string of decimal digits (0-9).Let’say the following is our number.int val = 467;Now to return the result as 0467, use the following decimal format specifier.val.ToString("D4")Let us see another example.Example Live Demousing System; using System.Globalization; class Demo {    static void Main() {       int val;       val = 877;       Console.WriteLine(val.ToString("D"));       Console.WriteLine(val.ToString("D4"));       Console.WriteLine(val.ToString("D8"));    } }Output877 0877 00000877

C# OverflowException

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

959 Views

OverflowException is thrown when the parameter value is out of integer ranges.Let us see an example.When we set a value to int.Parse() method that is out of integer range, then OverflowException is thrown as shown below −Example Live Demousing System; class Demo {    static void Main() {       string str = "757657657657657";       int res = int.Parse(str);    } }OutputThe following error is thrown when the above program is compiled since we have passed a value that is out of integer (Int32) range.Unhandled Exception: System.OverflowException: Value was either too large or too small for an Int32.

Get Upperbound and Lowerbound of a three-dimensional array in C#

Ankith Reddy
Updated on 23-Jun-2020 08:56:32

2K+ Views

To get the Upperbound and Lowerbound, use the GetUpperBound() GetLowerBound() methods in C#, respectively.The parameter to be set under these methods is the dimensions i.e.Let’s say our 3D array is −int[, , ] arr = new int[2, 3, 4];For a three-dimensional arrays, dimension 0.arr.GetUpperBound(0) arr.GetLowerBound(0)For a three-dimensional arrays, dimension 1.arr.GetUpperBound(1) arr.GetLowerBound(1)For a three-dimensional arrays, dimension 2.arr.GetUpperBound(2) arr.GetLowerBound(2)Example Live Demousing System; class Program {    static void Main() {       int[, , ] arr = new int[2, 3, 4];       Console.WriteLine("Dimension 0 Upper Bound: {0}", arr.GetUpperBound(0).ToString());       Console.WriteLine("Dimension 0 Lower Bound: {0}", arr.GetLowerBound(0).ToString());       ... Read More

C# DefaultIfEmpty Method

Samual Sam
Updated on 23-Jun-2020 08:56:58

288 Views

This method is used to handle empty collections. Instead of showing an error, this method displays a default value.We have the following list.List myList = new List();As you can see, since the above list is empty, we can display the default value.var res = myList.DefaultIfEmpty();Let us see an example.Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo {    static void Main() {       List myList = new List();       var res = myList.DefaultIfEmpty();       foreach (var a in res) {          Console.WriteLine(a);       }    } }Output0

C# Linq Count method

Arjun Thakur
Updated on 23-Jun-2020 08:57:19

670 Views

The Count method returns the count of elements in a sequence.Let us first set an array.string[] arr = { "Java", "C++", "Python"};Now, use the Count() method to count the array elements.arr.AsQueryable().Count();The following is the complete example.Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo {    static void Main() {       string[] arr = { "Java", "C++", "Python"};       int arr_count = arr.AsQueryable().Count();       Console.WriteLine("Count of arrays: {0}", arr_count);    } }OutputCount of arrays: 3

C# Program to search for a string in an array of strings

karthikeya Boyini
Updated on 23-Jun-2020 08:57:43

3K+ Views

Use Linq Contains() method to search for as specific string in an array of strings.string[] arr = { "Bag", "Pen", "Pencil"};Now, add the string in a string variable i.e. the string you want to search.string str = "Pen";Use the Contains() method to search the above string.arr.AsQueryable().Contains(str);Let us see the entire example.Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo {    static void Main() {       string[] arr = { "Bag", "Pen", "Pencil"};       string str = "Pen";       bool res = arr.AsQueryable().Contains(str);       Console.WriteLine("String Pen is in the array? "+res);   ... Read More

C# Linq Contains Method

Chandu yadav
Updated on 23-Jun-2020 08:58:04

3K+ Views

To check for an element in a string, use the Contains() method.The following is our string array.string[] arr = { "Java", "C++", "Python"};Now, use Contains() method to find a specific string in the string array.arr.AsQueryable().Contains(str);Let us see the complete example.Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo {    static void Main() {       string[] arr = { "Java", "C++", "Python"};       string str = "Python";       bool res = arr.AsQueryable().Contains(str);       Console.WriteLine("Array has Python? "+res);    } }OutputArray has Python? True

C# Cast method

Samual Sam
Updated on 23-Jun-2020 08:43:19

2K+ Views

To cast elements, use the Cast() method.The following is our list.List myList = new List { "Mac", "Windows", "Linux", "Solaris" };Now, cast and use the Cast() method with substring() method to display the first two letters of every string in the list.IEnumerable res = myList.AsQueryable().Cast().Select(str => str.Substring(0, 2));Let us see the complete example.Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo {    static void Main() {       List list = new List { "keyboard", "mouse", "joystick", "monitor" };       // getting first 2 letters from every string       IEnumerable res = list.AsQueryable().Cast().Select(str => ... Read More

C# Average Method

George John
Updated on 23-Jun-2020 08:43:44

22K+ Views

To find the average of integers in C#, use the Queryable Average() method.Let’s say the following is our integer array.var arr = new int[] { 10, 17, 25, 30, 40, 55, 60, 70 };Now, use the Average() method to get the average of the elements.double avg = Queryable.Average(arr.AsQueryable());Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       var arr = new int[] { 10, 17, 25, 30, 40, 55, 60, 70 };       double avg = Queryable.Average(arr.AsQueryable());       Console.WriteLine("Average = "+avg);    } }OutputAverage = 38.375

Implicit conversion from 32-bit unsigned integer (UInt) to Decimal in C#

karthikeya Boyini
Updated on 23-Jun-2020 08:44:10

818 Views

Implicit conversion of a 32-bit unsigned integer (UInt) to a Decimal requires you to first declare a UInt.uint val = 342741539;Now to convert it to decimal, just assign the value.decimal dec; // implicit dec = val;Example Live Demousing System; public class Demo {    public static void Main() {       uint val = 342741539;       decimal dec;       // implicit       dec = val;       Console.WriteLine("Decimal = "+dec);    } }OutputDecimal = 342741539

Advertisements