Chash All Method

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 element in the above array is greater than 5 or not.arr.AsQueryable().All(val => val > 5);Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       int[] arr = {10, 15, 20};       // checking if all the array elements are greater than 5       bool res = arr.AsQueryable().All(val => val > 5);       Console.WriteLine(res);    } }OutputTrue

Remove All Duplicates from a Given String in C#

Ankith Reddy
Updated on 22-Jun-2020 15:43:10

500 Views

Here is the string.string str = "ppqqrr";Now, use Hashset to map the string to char. This will remove the duplicate characters from a string.var res = new HashSet(str);Let us see the complete example −Example Live Demousing System; using System.Linq; using System.Collections.Generic; namespace Demo {    class Program {       static void Main(string[] args) {          string str = "ppqqrr";          Console.WriteLine("Initial String: "+str);          var res = new HashSet(str);          Console.Write("New String after removing duplicates:");          foreach (char c in res){             Console.Write(c);          }         }    } }OutputInitial String: ppqqrr New String after removing duplicates:pqr

C# LINQ Distinct Method

George John
Updated on 22-Jun-2020 15:42:04

861 Views

To get the distinct elements, use the Distinct() method.The following is our list with duplicate elements.List points = new List { 5, 10, 5, 20, 30, 30, 40, 50, 60, 70 };Now to get the distinct elements −points.AsQueryable().Distinct();Let us see the entire example −Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo {    static void Main() {       List points = new List { 5, 10, 5, 20, 30, 30, 40, 50, 60, 70 };       // distict elements from the list       IEnumerable res = points.AsQueryable().Distinct();       foreach (int a in res) {          Console.WriteLine(a);       }    } }Output5 10 20 30 40 50 60 70

C# Round-Trip R Format Specifier

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

955 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

Chash Hexadecimal X Format Specifier

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

9K+ 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# int.Parse vs int.TryParse Methods

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

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

657 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

324 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

281 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

164 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

Advertisements