Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Csharp Articles
Page 82 of 196
C# program to check if a Substring is present in a Given String
The Contains() method in C# is used to check if a substring is present within a given string. It returns a boolean value − true if the substring is found, and false otherwise. Syntax Following is the syntax for the Contains() method − bool result = string.Contains(substring); Parameters substring − The string to search for within the main string. Return Value The Contains()
Read MoreHow to get int value from enum in C#?
In C#, an enum (enumeration) is a value type that represents a group of named constants. By default, each enum value has an underlying integer value starting from 0. You can extract the integer value from an enum using type casting. Syntax Following is the syntax for casting an enum to int − int value = (int)EnumName.EnumValue; Following is the syntax for declaring an enum − public enum EnumName { Value1, Value2, Value3 } Using Default Integer Values By default, enum values are assigned integer values starting from 0 ...
Read MoreC# program to check if string is panagram or not
A pangram is a sentence that contains all 26 letters of the English alphabet at least once. The word "pangram" comes from Greek, meaning "all letters". A famous example is "The quick brown fox jumps over the lazy dog". In C#, we can check if a string is a pangram by converting it to lowercase, filtering only alphabetic characters, and counting the distinct letters present. Syntax The basic approach uses LINQ methods to process the string − string.ToLower().Where(ch => Char.IsLetter(ch)).GroupBy(ch => ch).Count() This checks if the count of distinct letters equals 26. ...
Read MoreC# Example for Single Inheritance
Single inheritance in C# allows a derived class to inherit from only one base class. This is the most common form of inheritance where a child class extends the functionality of its parent class by inheriting all accessible members and adding its own specific methods or properties. Syntax Following is the syntax for single inheritance in C# − class BaseClass { // base class members } class DerivedClass : BaseClass { // derived class members // inherits all accessible members from BaseClass } ...
Read MoreC# Generics vs C++ Templates
C# Generics and C++ Templates both provide support for parameterized types, allowing you to write reusable code that works with different data types. However, they differ significantly in their design philosophy, capabilities, and implementation approaches. Key Differences Between C# Generics and C++ Templates Feature C# Generics C++ Templates Compilation Model Compile-time and run-time support Compile-time only Type Safety Strong type checking at compile-time Duck typing, errors at instantiation Specialization No explicit or partial specialization Full support for specialization Non-type Parameters Not supported Fully supported ...
Read MoreC# Multiple Local Variable Declarations
In C#, you can declare and initialize multiple local variables of the same type in a single statement using the comma operator. This provides a concise way to declare several variables at once, making your code more readable and reducing repetition. Syntax Following is the syntax for declaring multiple local variables of the same type − dataType variable1 = value1, variable2 = value2, variable3 = value3; You can also declare variables without initialization − dataType variable1, variable2, variable3; Basic Multiple Variable Declaration The following example demonstrates declaring four integer ...
Read MoreHow to write single-line comments in C#?
Single-line comments in C# are created using two forward slashes (//). Everything after the // on that line is treated as a comment and is ignored by the compiler. These comments are useful for explaining code, adding notes, or temporarily disabling a line of code. Syntax Following is the syntax for single-line comments in C# − // This is a single-line comment int variable = 10; // Comment at the end of a line Using Single-Line Comments for Code Documentation Example using System; namespace Demo { class ...
Read MoreC# program to check for URL in a String
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 MoreBinary search in C#
The binary search algorithm works on the divide and conquer principle as it keeps dividing the array in half before searching. To search for an element in an array using binary search, the array must be sorted. In the sorted array, we find the middle element and compare it with the element that has to be searched, and based on the comparison, we either search in the left sub-array, right sub-array, or return the middle element. In this article, we are given a sorted array of integers, and our task is to search for the given target element ...
Read MoreC# program to check if a string contains any special character
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