Csharp Articles

Page 61 of 196

C# All method

Arjun Thakur
Arjun Thakur
Updated on 22-Jun-2020 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

Read More

C# Linq Distinct() Method

George John
George John
Updated on 22-Jun-2020 892 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

Read More

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

karthikeya Boyini
karthikeya Boyini
Updated on 22-Jun-2020 997 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

C# Hexadecimal ("X") Format Specifier

Samual Sam
Samual Sam
Updated on 22-Jun-2020 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

Read More

C# int.Parse Vs int.TryParse Method

Chandu yadav
Chandu yadav
Updated on 22-Jun-2020 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

How to generate a string randomly using C#?

Arjun Thakur
Arjun Thakur
Updated on 22-Jun-2020 682 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

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

George John
George John
Updated on 22-Jun-2020 299 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
Ankith Reddy
Updated on 22-Jun-2020 179 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 #region directive in C#?

Chandu yadav
Chandu yadav
Updated on 22-Jun-2020 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!

Read More

What is the operator that concatenates two or more string objects in C#?

Arjun Thakur
Arjun Thakur
Updated on 22-Jun-2020 673 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!

Read More
Showing 601–610 of 1,951 articles
« Prev 1 59 60 61 62 63 196 Next »
Advertisements