Found 2628 Articles for Csharp

C# Stack.TrimExcess() Method with Examples

AmitDiwan
Updated on 03-Dec-2019 10:35:34

480 Views

The Stack.TrimExcess() method in C# is used to set the capacity to the actual number of elements in the List, if that number is less than a threshold value.Syntaxpublic void TrimExcess ();Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main() {       Stack stack = new Stack();       stack.Push(100);       stack.Push(150);       stack.Push(175);       stack.Push(200);       stack.Push(225);       stack.Push(250);       stack.Push(300);       stack.Push(400);       stack.Push(450);       stack.Push(500);       Console.WriteLine("Elements in the ... Read More

Array.LastIndexOf() Method in C#

AmitDiwan
Updated on 03-Dec-2019 10:30:28

2K+ Views

The Array.LastIndexOf() method in C# is used to search for the specified object and returns the index of the last occurrence within the entire one-dimensional Array.Syntaxpublic static int LastIndexOf (Array arr, object val);Above, arr is the one-dimensional Array to search, whereas val is the object to locate in arr.Example Live Demousing System; public class Demo {    public static void Main() {       string[] strArr = {"John", "Tim", "Fedric", "Gary", "Harry", "Damien", "David", "Harry"};       Array.Sort(strArr);       Console.WriteLine("Array elements...");       foreach(string s in strArr) {          Console.WriteLine(s);       ... Read More

Array.BinarySearch(Array, Object) Method with examples in C#

AmitDiwan
Updated on 03-Dec-2019 10:27:25

128 Views

The Array.BinarySearch(Array, Object) method in C# is used to search an entire one-dimensional sorted array for a specific element, using the IComparable interface implemented by each element of the array and by the specified object.Syntaxpublic static int BinarySearch (Array arr, object val);Above, arr is the sorted 1-D array, whereas val is the object to search for.Example Live Demousing System; public class Demo {    public static void Main() {       int[] intArr = {5, 10, 15, 20};       Array.Sort(intArr);       Console.WriteLine("Array elements...");       foreach(int i in intArr) {          Console.WriteLine(i); ... Read More

C# Copy() Method

AmitDiwan
Updated on 03-Dec-2019 10:19:27

365 Views

The Copy() method in C# is used to create a new instance of String with the same value as a specified String.Syntaxpublic static string Copy (string s);Above, s is the string to copy.Example Live Demousing System; public class Demo {    public static void Main(string[] args) {       string s1 = "Amy";       string s2 = "Katie";       string s3 = String.Copy(s2);       Console.WriteLine("String1 = "+s1);       Console.WriteLine("String2 = "+s2);       Console.WriteLine("Are both the strings equal? = "+s1.Equals(s2));       Console.WriteLine("Are both the strings equal? = "+s2.Equals(s3));   ... Read More

C# CompareOrdinal() Method

AmitDiwan
Updated on 03-Dec-2019 10:16:17

216 Views

The CompareOrdinal() method in C# is used to compare two specified String objects by evaluating the numeric values of the corresponding Char objects in each string.Syntaxpublic static int CompareOrdinal (string str1, string str2);Above, str1 and str2 are the strings to compare.The return value is less than zero, str1 < str2. It is zero, if str1 = str2. Returns greater than zero, if str1 > str2.Example Live Demousing System; public class Demo {    public static void Main(string[] args) {       string s1 = "Amy";       string s2 = "Katie";       string s3 = s2;   ... Read More

StringBuilder.ToString() Method in C#

AmitDiwan
Updated on 03-Dec-2019 09:57:10

2K+ Views

The StringBuilder.ToString() method in C# is used to convert the value of a StringBuilder to a String.SyntaxThe syntax is as follows −public override string ToString (); public string ToString (int begnIndex, int len);Above, the parameter begnIndex is the starting position of the substring in this instance, whereas len is the length of the substring.ExampleLet us now see an example − Live Demousing System; using System.Text; public class Demo{    public static void Main(){       StringBuilder strBuilder = new StringBuilder("Katie");       Console.WriteLine("String = "+strBuilder.ToString());       Console.WriteLine("StringBuilder capacity = "+strBuilder.Capacity);       Console.WriteLine("StringBuilder length = "+strBuilder.Length); ... Read More

Random.Next() Method in C#

AmitDiwan
Updated on 03-Dec-2019 09:54:27

2K+ Views

The Random.Next() method in C# is used to return a non-negative random integer.SyntaxThe syntax is as follows −public virtual int Next (); public virtual int Next (int maxVal);Above, the maxVal parameter is the exclusive upper bound of the random number to be generated.ExampleLet us now see an example − Live Demousing System; public class Demo {    public static void Main(){       Random r = new Random();       Console.WriteLine("Random numbers.....");       for (int i = 1; i

SortedDictionary.Values Property in C#

AmitDiwan
Updated on 03-Dec-2019 09:52:19

91 Views

The SortedDictionary.Value property in C# is used to get a collection containing the values in the SortedDictionary.SyntaxThe syntax is as follows −public System.Collections.Generic.SortedDictionary.ValueCollection Values { get; }ExampleLet us now see an example − Live Demousing System; using System.Collections; using System.Collections.Generic; public class Demo {    public static void Main(){       SortedDictionarysortedDict = new SortedDictionary();       sortedDict.Add(1, "Ultrabook");       sortedDict.Add(2, "Alienware");       sortedDict.Add(3, "Notebook");       sortedDict.Add(4, "Connector");       sortedDict.Add(5, "Flash Drive");       sortedDict.Add(6, "SSD");       sortedDict.Add(7, "HDD");       sortedDict.Add(8, "Earphone");       Console.WriteLine("SortedDictionary ... Read More

Double.IsNegativeInfinity() Method in C#

AmitDiwan
Updated on 03-Dec-2019 09:48:11

32 Views

The Double.IsNegativeInfinity() method in C# is used to return a value indicating whether the specified number evaluates to negative infinity.SyntaxThe syntax is as follows −public static bool IsNegativeInfinity (double val);Above, val is a double-precision floating-point number.ExampleLet us now see an example − Live Demousing System; public class Demo {    public static void Main(){       double d = 0.0/0;       Console.WriteLine("Double Value = "+d);       Console.WriteLine("HashCode of Double Value = "+d.GetHashCode());       TypeCode type = d.GetTypeCode();       Console.WriteLine("TypeCode of Double Value = "+type);       Console.WriteLine("Positive Infinity? = "+Double.IsInfinity(d));   ... Read More

Boolean.GetTypeCode() Method in C# with Examples

AmitDiwan
Updated on 03-Dec-2019 09:46:18

42 Views

The Boolean.GetTypeCode() method in C# is used to return the type code for the Boolean value type.SyntaxThe syntax is as follows −public TypeCode GetTypeCode ();ExampleLet us now see an example − Live Demousing System; public class Demo {    public static void Main(String[] args){       string str = "JackSparrow!";       bool val = true;       char[] arr = { 'J', 'a'};       Console.WriteLine("String = "+str);       Console.WriteLine("String (after trim) = " + str.Trim(arr));       Console.WriteLine("String (Hashcode) = "+str.GetHashCode());       Console.WriteLine("Bool (Hashcode) = "+val.GetHashCode());       Console.WriteLine("Bool ... Read More

Advertisements