Samual Sam

Samual Sam

1,507 Articles Published

Articles by Samual Sam

Page 19 of 151

What is the difference between String and string in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 663 Views

In C#, string and String are functionally identical − string is simply an alias for System.String. Both refer to the same .NET type and can be used interchangeably in your code. Syntax Both declarations are equivalent − string str1 = "Hello World"; String str2 = "Hello World"; Both can use static methods from the String class − string result1 = string.Format("Hello {0}", name); string result2 = String.Format("Hello {0}", name); Key Differences string (lowercase) String (uppercase) C# keyword and alias Actual .NET class ...

Read More

Find a specific element in a C# List

Samual Sam
Samual Sam
Updated on 17-Mar-2026 343 Views

In C#, you can find specific elements in a List using various built-in methods. The most common approach is using the Find() method with a lambda expression to specify search criteria. Syntax Following is the syntax for using the Find() method − T element = list.Find(predicate); Where predicate is a lambda expression that returns true for the element you want to find. Using Find() Method The Find() method returns the first element that matches the specified condition. If no element is found, it returns the default value for the type (0 for ...

Read More

C# program to check for URL in a String

Samual Sam
Samual Sam
Updated on 17-Mar-2026 3K+ Views

In C#, you can check for URLs in a string using various methods such as StartsWith() for simple prefix matching or regular expressions for more comprehensive URL validation. The StartsWith() method is useful when you need to verify if a string begins with a specific URL pattern. Syntax Following is the syntax for using StartsWith() to check URL prefixes − string.StartsWith("url_prefix") For checking multiple URL patterns, you can combine conditions using logical operators − if (input.StartsWith("https://www.") || input.StartsWith("https://")) { // URL found } Using StartsWith() for Simple ...

Read More

C# SingleorDefault() Method

Samual Sam
Samual Sam
Updated on 17-Mar-2026 4K+ Views

The SingleOrDefault() method in C# returns a single specific element from a sequence. If no element is found, it returns the default value for the type. If multiple elements are found, it throws an exception. This method is part of LINQ and can be used with any IEnumerable collection. It's particularly useful when you expect exactly zero or one element in the result. Syntax Following is the basic syntax for SingleOrDefault() − public static TSource SingleOrDefault(this IEnumerable source) With a predicate condition − public static TSource SingleOrDefault(this IEnumerable source, Func predicate) ...

Read More

How to define custom methods in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 1K+ Views

To define a custom method in C#, you create a reusable block of code that performs a specific task. Methods help organize your code, make it more readable, and avoid repetition by allowing you to call the same functionality multiple times. Syntax Following is the syntax for defining a custom method in C# − (Parameter List) { Method Body } Method Components The following are the various elements of a method − Access Specifier − This determines the visibility of a method from other classes. ...

Read More

ContainsValue in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 145 Views

The ContainsValue() method in C# is used to determine whether a Dictionary contains a specific value. It returns true if the value exists in the dictionary, and false otherwise. This method performs a sequential search through all values in the dictionary. Syntax Following is the syntax for using the ContainsValue() method − bool result = dictionary.ContainsValue(value); Parameters value − The value to search for in the dictionary. The type must match the value type of the dictionary. Return Value Returns true if the dictionary contains the specified value; otherwise, ...

Read More

How to sort a list of dictionaries by values of dictionaries in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 542 Views

Sorting a list of dictionaries by their values is a common requirement in C# programming. There are multiple approaches to achieve this, ranging from sorting by keys to sorting by values, and even sorting lists containing multiple dictionaries. Syntax Following is the syntax for sorting dictionary entries by values using LINQ − var sortedByValue = dictionary.OrderBy(x => x.Value); var sortedByValueDesc = dictionary.OrderByDescending(x => x.Value); For sorting by keys − var sortedByKey = dictionary.OrderBy(x => x.Key); Sorting Dictionary by Keys The simplest approach is to sort dictionary entries by their ...

Read More

C# program to check if a string contains any special character

Samual Sam
Samual Sam
Updated on 17-Mar-2026 12K+ Views

To check if a string contains any special character in C#, you can use the Char.IsLetterOrDigit method. This method returns false for any character that is not a letter or digit, which means it's a special character. Special characters include symbols like @, #, $, %, &, punctuation marks, and whitespace characters that are not alphanumeric. Syntax Following is the syntax for checking if a character is a letter or digit − bool Char.IsLetterOrDigit(char c) To check for special characters, use the negation operator − if (!Char.IsLetterOrDigit(character)) { ...

Read More

Case-insensitive Dictionary in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 3K+ Views

A case-insensitive Dictionary in C# allows you to perform key lookups without considering the case of string keys. This means keys like "cricket", "CRICKET", and "Cricket" are treated as identical. This is particularly useful when dealing with user input or data from external sources where case consistency cannot be guaranteed. Syntax To create a case-insensitive Dictionary, use the StringComparer.OrdinalIgnoreCase parameter in the constructor − Dictionary dict = new Dictionary(StringComparer.OrdinalIgnoreCase); You can also use other string comparers − // Culture-insensitive comparison Dictionary dict1 = new Dictionary(StringComparer.InvariantCultureIgnoreCase); // Current culture ignore case Dictionary ...

Read More

C# program to check password validity

Samual Sam
Samual Sam
Updated on 17-Mar-2026 986 Views

While creating a password, you may have seen validation requirements on websites that ensure a password is strong and secure. Common password requirements include − Minimum 8 characters and maximum 14 characters At least one lowercase letter No whitespace characters At least one uppercase letter At least one special character Let us create a complete password validation program that checks all these conditions systematically. Complete Password Validation Program using System; using System.Linq; class PasswordValidator { public static bool IsValidPassword(string passwd) { ...

Read More
Showing 181–190 of 1,507 articles
« Prev 1 17 18 19 20 21 151 Next »
Advertisements