Found 34490 Articles for Programming

Tuple.Create method in C#

George John
Updated on 22-Jun-2020 14:45:08

83 Views

To create a tuple, use the Tuple.Create method.Here we have set a tuple with a string and int −var myTuple = Tuple.Create("marks", 100);The following is an example that creates a tuple using Tuple.Create and displays the elements in a single line in C# −Example Live Demousing System; class Demo {    static void Main() {       var myTuple = Tuple.Create("marks", 100);       Console.WriteLine(myTuple);    } }Output(marks, 100)

C# Program to write a number in hexadecimal format

Ankith Reddy
Updated on 22-Jun-2020 14:45:32

403 Views

Let’s say the following is the number −int a = 12250;You can work around the following ways to get a number in hexadecimal format −{0:x} {0:x8} {0:X} {0:X8}Here is the code −Example Live Demousing System; class Demo {    static void Main() {       int a = 12250;       Console.WriteLine("{0:x}", a);       Console.WriteLine("{0:x8}", a);       Console.WriteLine("{0:X}", a);       Console.WriteLine("{0:X8}", a);    } }Output2fda 00002fda 2FDA 00002FDA

C# Program to split parts in a Windows directory

Arjun Thakur
Updated on 22-Jun-2020 14:46:00

452 Views

Firstly, set a string i.e. your Windows directory path −string str = @"D:\Downloads\Amit";Now use the Split() method and split wherever the \ occur −str.Split(' \')The following is the complete code −Example Live Demousing System; class Program {    static void Main() {       string str = @"D:\Downloads\Amit";       Console.WriteLine("Directory..."+str);       string[] myStr = str.Split('\');       Console.WriteLine("Split...");       foreach (string ch in myStr) {          Console.WriteLine(ch);       }    } }OutputDirectory... D:\Downloads\Amit Split... D: Downloads Amit

C# Program to split a string on spaces

Chandu yadav
Updated on 22-Jun-2020 14:46:25

2K+ Views

Firstly, set a string −string str = "Science and Mathematics";Now use the Split() method to split wherever the spaces occur −str.Split(' ')The following is the complete code −Example Live Demousing System; using System.Linq; using System.IO; class Program {    static void Main() {       string str = "Science and Mathematics";       Console.WriteLine("String..."+str);       string[] myStr = str.Split(' ');       Console.WriteLine("Splitted String...");       foreach (string ch in myStr) {          Console.WriteLine(ch);       }    } }OutputString... Science and Mathematics Splitted String... Science and Mathematics

C# Program to display a string in reverse alphabetic order

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

646 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() {       string str = "Amit";       char[] arr = str.ToCharArray();       Console.WriteLine("Original String: "+str);       // Reverse       Array.Reverse(arr);       Console.WriteLine("Reversed String: "+new string(arr));    } }OutputOriginal String: Amit Reversed String: timA

Get the number of bytes in a file in C#

Ankith Reddy
Updated on 07-Apr-2020 10:04:34

326 Views

FileInfo type has a Length property that determines how many bytes a file has.Firstly, set the file −FileInfo file = new FileInfo("D:ew");Now use the Length property −file.LengthHere is the complete code −Exampleusing System; using System.Linq; using System.IO; class Program {    static void Main() {       FileInfo file = new FileInfo("D:ew");       long res = file.Length;       Console.WriteLine("Bytes: "+res);    } }OutputThe following is the output −3259244

List files recursively in C#

George John
Updated on 07-Apr-2020 10:06:49

856 Views

To get the list of files in a directory, use the SearchOptions.AllDirectories in C#.Firstly, set the directory for which you want the files −string[] myFiles = Directory.GetFiles("D:\New\", "*.*", SearchOption.AllDirectories);The following is an example displaying files from the above mentioned directory −Exampleusing System; using System.Linq; using System.IO; class Program {    static void Main() {       string[] myFiles = Directory.GetFiles("D:\New\", "*.*", SearchOption.AllDirectories);       foreach (string res in myFiles) {          Console.WriteLine(res);       }    } }OutputThe following is the output. It lists all the directories of the folder −D:\New\one.txt D:\New\two.html D:\Newature.png

SequenceEqual method in C#

Ankith Reddy
Updated on 22-Jun-2020 14:47:37

264 Views

The SequenceEqual method is used to test collections for equality.Let us set three string arrays −string[] arr1 = { "This", "is", "it" }; string[] arr2 = { "My", "work", "report" }; string[] arr3 = { "This", "is", "it" };Now, compare the first array with the second using the SequenceEqual() method −arr1.SequenceEqual(arr2);The following is an example −Example Live Demousing System; using System.Linq; class Program {    static void Main() {       string[] arr1 = { "This", "is", "it" };       string[] arr2 = { "My", "work", "report" };       string[] arr3 = { "This", "is", "it" };       bool res1 = arr1.SequenceEqual(arr2);       Console.WriteLine(res1);       bool res2 = arr1.SequenceEqual(arr3);       Console.WriteLine(res2);    } }OutputFalse True

Any Extension method in C#

George John
Updated on 22-Jun-2020 14:47:54

359 Views

The Any() extension method is part of the System.Linq namepspace. Using this method, you can check whether any of the elements matches a certain condition or not.Firstly, set an array with elements −int[] arr = { 6, 7, 15, 40, 55 };The following is an example. It checks whether any of the element in the array is greater than and equal to 20 or not −arr.Any(element => element >= 20);Here is the complete code −Example Live Demousing System; using System.Linq; class Program {    static void Main() {       int[] arr = { 6, 7, 15, 40, 55 };       bool res = arr.Any(element => element >= 20);       Console.WriteLine(res);    } }OutputTrue

C# Program to get the first three elements from a list

Samual Sam
Updated on 22-Jun-2020 14:39:30

8K+ Views

Use the Take() method to get the first individual number of elements in C#.Firstly, set a list and add elements −List myList = new List(); myList.Add("One"); myList.Add("Two"); myList.Add("Three"); myList.Add("Four"); myList.Add("Five"); myList.Add("Six");Now, use the Take() method to get the elements from the list. Add the number of the elements you want as an argument −myList.Take(3)Here is the code −Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       List myList = new List();       myList.Add("One");       myList.Add("Two");       myList.Add("Three");       myList.Add("Four");     ... Read More

Advertisements