C# int.Parse vs int.TryParse Methods

Chandu yadav
Updated on 22-Jun-2020 15:38:27

5K+ Views

Convert a string representation of number to an integer, using the int.TryParse and intParse method in C#.If the string cannot be converted, then the int.TryParse method returns false i.e. a Boolean value, whereas int.Parse returns an exception.Let us see an example of int.Parse method −Example Live Demousing System.IO; using System; class Program {    static void Main() {       int res;       string myStr = "120";       res = int.Parse(myStr);       Console.WriteLine("String is a numeric representation: "+res);    } }OutputString is a numeric representation: 120Let us see an example of int.TryParse method.Example Live Demousing ... Read More

Generate Random String using C#

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

640 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

Send Result of Python CGI Script to Browser

Arnab Chakraborty
Updated on 22-Jun-2020 15:37:06

310 Views

# Get data from fields from HTML page first_name = form.getvalue('first_name') last_name  = form.getvalue('last_name') send data to Browser print("Content-type:text/html") print print("") print("") print("Hello - Second CGI Program") print("") print("") print(" Hello %s %s " % (first_name, last_name)) print("") print("")

Why is a Dictionary Preferred Over a Hashtable in C#

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

260 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

Create a ValueType with Names in C#

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

154 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

Where to Use #pragma 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!

Operator That Concatenates String Objects in C#

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

653 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 using + operator.Example Live Demousing System.Text; using System; class Program {    static void Main() {       char[] c1 = { 'H', 'e', 'n', 'r', 'y' };       string str1 = new string(c1);       char[] c2 = { 'J', 'a', 'c', 'k' };       string str2 = new string(c2);       Console.WriteLine("Welcome " + str1 + " and " + str2 + "!");    } }OutputWelcome Henry and Jack!

Find Missing Number in a Sequence in C#

Ankith Reddy
Updated on 22-Jun-2020 15:33:16

1K+ Views

Set a list.List myList = new List(){1, 2, 3, 5, 8, 9};Now, get the first and last elements −int a = myList.OrderBy(x => x).First(); int b = myList.OrderBy(x => x).Last();In a new list, get all the elements and use the Except to get the missing numbers −List myList2 = Enumerable.Range(a, b - a + 1).ToList(); List remaining = myList2.Except(myList).ToList();Let us see the complete code −Example Live Demousing System.Collections.Generic; using System; using System.Linq; public class Program {    public static void Main() {       List myList = new List(){1, 2, 3, 5, 8, 9};       Console.WriteLine("Numbers... "); ... Read More

Role of CSS :last-child Selector

George John
Updated on 22-Jun-2020 15:32:37

259 Views

Use the CSS :last-child selector to style every elements that is the last child of its parent. You can try to run the following code to implement the :last-child selectorLive Demo                    p:last-child {             background: orange;          }                     This is demo text1.       This is demo text2.       This is demo text3.    

DirectoryNotFoundException in C#

Samual Sam
Updated on 22-Jun-2020 15:32:08

836 Views

If a directory you are trying to find does not exist, then DirectoryNotFoundException occurs.Here, we are try to find a directory that does not exist using GetDirectories() method.Exampleusing System.IO; using System; class Program {    static void Main() {       Directory.GetDirectories("D:ew\");    } }The above code will generate the following exception since the directory “D:ew” does not exist.Unhandled Exception: System.IO.DirectoryNotFoundException: Could not find a part of the path

Advertisements