Arjun Thakur has Published 1109 Articles

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

175 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

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

500 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

What is the operator that concatenates two or more string objects in C#?

Arjun Thakur

Arjun Thakur

Updated on 22-Jun-2020 15:34:05

566 Views

Use operator + to concatenate two or more string objects.Set the first string object.char[] c1 = { 'H', 'e', 'n', 'r', 'y' }; string str1 = new string(c1);Now, set the second string object.char[] c2 = { 'J', 'a', 'c', 'k' }; string str2 = new string(c2);Now display the concatenated strings ... Read More

C# program to get the extension of a file in C#

Arjun Thakur

Arjun Thakur

Updated on 22-Jun-2020 15:03:30

230 Views

To handle file paths, the Path class is used in C#.Set the file name in a string −string myPath = "D:ew\quiz.txt";Now, to get the extension, use the GetExtension() method −Path.GetExtension(myPath)Here is the complete code −Example Live Demousing System; using System.IO; namespace Demo {    class Program {       static ... Read More

Environment.NewLine in C#

Arjun Thakur

Arjun Thakur

Updated on 22-Jun-2020 15:02:36

1K+ Views

The Enviornment.NewLine in C# is used to add newline.To set a new line in between words −str = "This is demo text!" + Environment.NewLine + "This is demo text on next line!";The following is the code −Example Live Demousing System; using System.IO; namespace Demo {    class Program {     ... Read More

Randomize string in C#

Arjun Thakur

Arjun Thakur

Updated on 22-Jun-2020 14:54:50

1K+ Views

To randomize string, firstly use Random class −Random r = new Random();Now, use the Next() method with OrderBy() −string random = new string(str.ToCharArray().OrderBy(s => (r.Next(2) % 2) == 0).ToArray());Here is the compete code that displays randomize string −Example Live Demousing System; using System.IO; using System.Linq; class Demo {    static void ... Read More

Replace parts of a string with C# Regex

Arjun Thakur

Arjun Thakur

Updated on 22-Jun-2020 14:52:47

2K+ Views

Set a string −string str = "Bit and Bat";Let’s say you need to replace whatever comes inside B and t to A and capitalize the complete string. For that, use Replace −Regex.Replace(str, "B.t", "BAT");Let us see the complete code −Example Live Demousing System; using System.Text.RegularExpressions; namespace Demo {    class Program ... Read More

C# Program to display a string in reverse alphabetic order

Arjun Thakur

Arjun Thakur

Updated on 22-Jun-2020 14:46:49

640 Views

Set a string array and convert it to character array −string str = "Amit"; char[] arr = str.ToCharArray();Now, use the Reverse method to display the above string in reverse alphabetic order −Array.Reverse(arr);Here is the complete code −Example Live Demousing System; using System.Linq; using System.IO; class Program {    static void Main() ... Read More

Advertisements