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 128 of 196
Global and Local Variables in C#
In C#, variables are classified by their scope − the region of code where they can be accessed. The two main types are local variables (declared within methods) and global variables (which C# handles through static class members and namespace aliases). Local Variables A local variable is declared within a method, constructor, or block of code. Its scope is limited to that specific block, meaning it can only be accessed within the method or block where it was declared. Syntax dataType variableName; // or dataType variableName = value; Example using System; ...
Read MoreC# equivalent to Java's Thread.setDaemon?
C# equivalent to Java's Thread.setDaemon is the concept of foreground and background threads. In C#, background threads serve the same purpose as daemon threads in Java − they run in the background and are automatically terminated when all foreground threads complete. When all foreground threads terminate, the CLR (Common Language Runtime) automatically shuts down the application, causing all background threads to be terminated immediately. Foreground threads keep the application alive until they complete execution. Syntax Following is the syntax to create a background thread using the IsBackground property − Thread thread = new Thread(methodName); thread.IsBackground ...
Read MoreC# Equivalent to Java Functional Interfaces
The C# equivalent to Java's functional interfaces is delegates. Delegates in C# provide the same functionality as functional interfaces in Java, allowing you to treat methods as first-class objects and use lambda expressions for concise code. Java functional interfaces define a contract with a single abstract method, while C# delegates directly represent method signatures that can be assigned lambda expressions or method references. Syntax Following is the syntax for declaring a delegate in C# − public delegate ReturnType DelegateName(ParameterType parameter); Following is the syntax for assigning a lambda expression to a delegate − ...
Read MoreC# Equivalent to Java's Double Brace Initialization?
Java's Double Brace Initialization is a technique that allows creating and initializing collections in a single expression using an anonymous inner class. C# provides equivalent functionality through Collection Initializers and Object Initializers, which offer cleaner and more efficient syntax. Java Double Brace Initialization In Java, double brace initialization uses nested braces where the outer braces create an anonymous class and the inner braces contain an instance initializer − List list = new List() {{ add("One"); add("Two"); add("Three"); add("Four"); }} ...
Read MorePrint first letter of each word in a string using C# regex
In C#, you can use regular expressions to extract the first letter of each word from a string. This technique uses the Regex.Matches() method with a specific pattern to identify word boundaries and capture the initial characters. Syntax The regular expression pattern to match the first letter of each word is − @"\b[a-zA-Z]" Where − \b − represents a word boundary [a-zA-Z] − matches any single letter (uppercase or lowercase) The Regex.Matches() method syntax − MatchCollection matches = Regex.Matches(inputString, pattern); How It Works ...
Read MorePrint number with commas as 1000 separators in C#
In C#, you can format numbers with commas as thousand separators using various approaches. The most common and efficient methods involve using format strings with the ToString() method or composite formatting. Syntax Following is the syntax for formatting numbers with thousand separators − number.ToString("N") // Standard numeric format number.ToString("#, ##0.##") // Custom format with commas string.Format("{0:N}", number) // Composite formatting Using Standard Numeric Format ("N") The "N" format specifier automatically adds thousand separators and formats the number according to the current ...
Read MorePrint first m multiples of n in C#
To print the first m multiples of a given number n in C#, we use a loop to iterate and calculate each multiple. This is useful for displaying multiplication tables or generating sequences. Syntax Following is the basic syntax for printing multiples using a loop − for (int i = 1; i
Read MoreC# program to multiply all numbers in the list
Multiplying all numbers in a list is a common programming task in C#. This can be achieved using several approaches including loops, LINQ, and recursion. The key is to initialize a variable to 1 (the multiplicative identity) and then multiply each element in the list. Syntax Following is the basic syntax for multiplying all numbers in a list using a foreach loop − List numbers = new List { 1, 2, 3 }; int product = 1; foreach (int number in numbers) { product *= number; } Using Foreach Loop ...
Read MoreC# program to check for a string that contains all vowels
A C# program to check if a string contains all vowels involves examining the string to identify which vowels (a, e, i, o, u) are present. This is useful for word games, text analysis, or linguistic applications where you need to verify vowel completeness. Syntax The basic approach uses LINQ methods to filter and check for vowels − var vowels = str.Where(ch => "aeiouAEIOU".Contains(ch)).Distinct(); To check if all five vowels are present − bool hasAllVowels = vowels.Count() == 5; Using LINQ to Find All Vowels This approach uses the ...
Read MoreHow to convert a number from Decimal to Binary using recursion in C#?
Converting a decimal number to binary using recursion in C# involves repeatedly dividing the number by 2 and collecting the remainders. The recursive approach breaks down the problem into smaller subproblems until the base case is reached. Syntax Following is the basic syntax for the recursive binary conversion method − public void ConvertToBinary(int decimalNumber) { if (decimalNumber > 0) { ConvertToBinary(decimalNumber / 2); Console.Write(decimalNumber % 2); } } How It ...
Read More