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
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
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
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
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
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
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
# 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("")
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
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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP