Found 34490 Articles for Programming

Where to use #region directive in C#?

Chandu yadav
Updated on 22-Jun-2020 15:34:39

4K+ Views

It lets you specify a block of code that you can expand or collapse when using the outlining feature of the Visual Studio Code Editor. It should be terminated with #endregion.Let us see how to define a region using #region.#region NewClass definition public class NewClass {    static void Main() { } } #endregionThe following is an example showing the usage of #region directive.Example Live Demousing System; #region class MyClass { } #endregion class Demo {    #region VARIABLE    int a;    #endregion    static void Main() {       #region BODY       Console.WriteLine("Example showing the usage of region directive!");       #endregion    } }OutputExample showing the usage of region directive!

Why is a Dictionary preferred over a Hashtable in C#?

George John
Updated on 22-Jun-2020 15:36:35

177 Views

Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. It uses the key to access the elements in the collection.Dictionary is a collection of keys and values in C#. Dictionary is included in the System.Collection.Generics namespace.Hashtable is slower than Dictionary. For strongly-typed collections, the Dictionary collection is faster.Let’s say we need to find a key from Hashtable collections. With that, we are also finding a key from Dictionary collection as well. In that case, Dictionary would be faster for the same statement −For HashTablehashtable.ContainsKey("12345");For Dictionarydictionary.ContainsKey("12345") Read More

C# program to create a ValueType with names

Ankith Reddy
Updated on 22-Jun-2020 15:35:11

106 Views

With C# 7, you can easily create a ValueType with names.Note − Add System.ValueTuple package to run ValueTuple program.Let’s see how to add it −Go to your projectRight click on the project in the solution explorerSelect “Manage NuGet Packages”You will reach the NuGet Package Manager.Now, click the Browse tab and find “ValueTuple”Finally, add System.ValueTuple packageExampleusing System; class Program {    static void Main() {       var myTuple = (marks: 95, name: "jack", subject: "maths");       //Add System.ValueTuple package to run this program       // using names to access       Console.WriteLine("Student Marks: "+myTuple.marks); ... Read More

How to generate a string randomly using C#?

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

501 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, a random character is generated and appended to form a string.The following is the complete example −Example Live Demousing System.Text; using System; class Program {    static void Main() {       StringBuilder str = new StringBuilder();       char c;       Random random = new Random((int)DateTime.Now.Ticks); ... Read More

C# Hexadecimal ("X") Format Specifier

Samual Sam
Updated on 22-Jun-2020 15:39:09

7K+ Views

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

C# Round-trip ("R") Format Specifier

karthikeya Boyini
Updated on 22-Jun-2020 15:39:31

637 Views

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

Convert a ValueTuple to a Tuple in C#

Samual Sam
Updated on 22-Jun-2020 15:27:27

386 Views

With C#, we can easily convert a ValueTuple to a Tuple using ToTuple() method.Note − Add System.ValueTuple package to run ValueTuple program.Let’s see how to add it −Go to your projectRight click on the project in the solution explorerSelect “Manage NuGet Packages”You will reach the NuGet Package Manager.Now, click the Browse tab and find “ValueTuple”Finally, add System.ValueTuple packageExampleusing System; class Program {    static void Main() {       var val = (5, 50, 500, 5000);       //Add System.ValueTuple package to run this program       // ValueTuple       Console.WriteLine(“ValueTuple: ” val);       // Tuple       Tuple myTuple = val.ToTuple();       Console.WriteLine(“Tuple: ”+myTuple);    } }OutputValueTuple: (5, 50, 500, 5000) Tuple: (5, 50, 500, 5000)

Represent Int32 as a Binary String in C#

karthikeya Boyini
Updated on 22-Jun-2020 15:27:53

6K+ Views

To represent Int632as 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.Int32 represents a 32-bit signed integer.Firstly, set an Int64 variable −int val = 30;Now, convert it to a binary string by including 2 as the second parameter.Convert.ToString(val, 2)Example Live Demousing System; class Demo {    static void Main() {       int val = 30;       Console.WriteLine("Integer: "+val);       Console.Write("Binary String: "+Convert.ToString(val, 2));    } }OutputInteger: 30 Binary String: 11110

How to get complete drive information using C#?

Samual Sam
Updated on 08-Apr-2020 09:33:56

165 Views

Drive information of an Operating System includes.Drive Name Volume Label Free Space Total Size Drive Format Drive TypeTo get above information about a drive, try to run the following code −Exampleusing System.IO; using System; class Program {    static void Main() {       DriveInfo driveInfo = new DriveInfo("D");       Console.WriteLine(driveInfo.Name);       Console.WriteLine(driveInfo.VolumeLabel);       Console.WriteLine(driveInfo.AvailableFreeSpace);       Console.WriteLine(driveInfo.TotalFreeSpace);       Console.WriteLine(driveInfo.TotalSize);       Console.WriteLine(driveInfo.DriveFormat);       Console.WriteLine(driveInfo.DriveType);    } }OutputThe following is the output −D: NTFS 76767677788 76767677788 45463434799 NTFS FixedNote − The output may vary with different Operating Systems.

C# Program to remove whitespaces in a string

karthikeya Boyini
Updated on 22-Jun-2020 15:28:33

7K+ Views

Let’s say the following is the string −StringBuilder str = new StringBuilder("Patience is key!");To remove whitespace, you can use the replace method.str.Replace(" ", "");Let us see the complete code.Example Live Demousing System; using System.Text; class Demo {    static void Main() {       // Initial String       StringBuilder str = new StringBuilder("Patience is key!");       Console.WriteLine(str.ToString());       // Replace       str.Replace(" ", "");       // New String       Console.WriteLine(str.ToString());       Console.ReadLine();    } }OutputPatience is key! Patienceiskey!

Advertisements