Arjun Thakur has Published 1025 Articles

C# Program to return specified number of elements from the end of an array

Arjun Thakur

Arjun Thakur

Updated on 23-Jun-2020 07:28:31

194 Views

Use the TakeLast() method to return elements from the end of an array.Let us first declare and initialize an array.int[] prod = { 110, 290, 340, 540, 456, 698, 765, 789};Now, let’s get the last three elements.IEnumerable units = prod.AsQueryable().TakeLast(3);Let us see the complete code.Example Live Demousing System; using System.Linq; using ... Read More

C# Program to cast a type to its IEnumerable equivalent

Arjun Thakur

Arjun Thakur

Updated on 23-Jun-2020 07:21:04

431 Views

Use the AsEnumerable() method to cast a type to its IEnumerable equivalent. It is an extension method.For our example, we have set an array.int[] myArr = new int[10]; myArr[0] = 1; myArr[1] = 2; myArr[2] = 3; myArr[3] = 4; myArr[4] = 5;Now, we have used the AsEnumerable() method to ... Read More

C# Program to check a string for whitespace characters or null

Arjun Thakur

Arjun Thakur

Updated on 23-Jun-2020 07:20:16

709 Views

This method returns true if the entered string has only whitespace characters or is null.Let’s say you are checking for whitespace character.bool val1 = string.IsNullOrWhiteSpace(“ “);The above returns True, since the string is a whitespace character. In the same way, check it for null using the IsNullOrWhiteSpace() method.Here is the ... Read More

C# OfType() Method

Arjun Thakur

Arjun Thakur

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

599 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 = ... Read More

Month ("M", "m") Format Specifier in C#

Arjun Thakur

Arjun Thakur

Updated on 23-Jun-2020 07:05:26

346 Views

The Month standard format specifier represents a custom date and time format string.The format string is defined by the current DateTimeFormatInfo.MonthDayPattern property.The custom format string −MMMM ddExample Live Demousing System; using System.Globalization; class Demo {    static void Main() {       DateTime date = new DateTime(2018, 6, 11, 9, ... Read More

Why is f required while declaring floats in C#?

Arjun Thakur

Arjun Thakur

Updated on 23-Jun-2020 06:59:13

751 Views

The f is a lowercase suffix set while declaring a float. It tells the compiler that the literal is of a specific type.Example Live Demousing System.IO; using System; public class Program {    public static void Main() {       float val = 30.22f;       Console.WriteLine(val);    } ... Read More

Set tuple as a method parameter in C#

Arjun Thakur

Arjun Thakur

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

4K+ 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 ... Read More

Convert.ToUInt16 Method in C#

Arjun Thakur

Arjun Thakur

Updated on 22-Jun-2020 15:45:35

250 Views

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() ... Read More

C# All method

Arjun Thakur

Arjun Thakur

Updated on 22-Jun-2020 15:43:53

3K+ Views

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 ... Read More

How to generate a string randomly using C#?

Arjun Thakur

Arjun Thakur

Updated on 22-Jun-2020 15:37:33

629 Views

Firstly, set a string.StringBuilder str = new StringBuilder();Use Random.Random random = new Random((int)DateTime.Now.Ticks);Now loop through a number which is the length of the random string you want.for (int i = 0; i < 4; i++) {    c = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));    str.Append(c); }On every iteration above, ... Read More

Advertisements