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
Programming Articles
Page 831 of 2547
C# Program to display the name of the directory
In C# programming, you can display the name of a directory using the DirectoryInfo class from the System.IO namespace. This class provides properties and methods to work with directory information, including retrieving just the directory name without the full path. Syntax Following is the syntax for creating a DirectoryInfo object and accessing the directory name − DirectoryInfo dir = new DirectoryInfo(directoryPath); string directoryName = dir.Name; Parameters directoryPath − A string representing the path to the directory Return Value The Name property returns a string containing only the name of ...
Read MoreC# Program to return the difference between two sequences
In C#, you can find the difference between two sequences using the Except() method from LINQ. This method returns elements from the first sequence that do not appear in the second sequence, effectively performing a set difference operation. Syntax Following is the syntax for using the Except() method − IEnumerable result = firstSequence.Except(secondSequence); Parameters firstSequence − The sequence to return elements from. secondSequence − The sequence whose elements to exclude from the returned sequence. Return Value The method returns an IEnumerable containing the elements from the first sequence that ...
Read MoreType.GetNestedTypes() Method in C#
The Type.GetNestedTypes() method in C# is used to get the types nested within the current Type. This method is particularly useful when working with reflection to discover and work with nested classes, interfaces, or other types defined within a class. Syntax Following is the syntax for the GetNestedTypes() method − public Type[] GetNestedTypes(); public abstract Type[] GetNestedTypes(System.Reflection.BindingFlags bindingAttr); Parameters The overloaded version accepts a BindingFlags parameter that specifies which nested types to return − bindingAttr − A combination of BindingFlags values that control which nested types are returned (e.g., Public, NonPublic, ...
Read MoreC# program to count occurrences of a word in string
Counting occurrences of a specific word in a string is a common string manipulation task in C#. This can be achieved using various approaches like the IndexOf() method, regular expressions, or LINQ methods. Using IndexOf() Method The IndexOf() method searches for the first occurrence of a substring and returns its position. By combining it with a loop, we can count all occurrences − using System; class Program { static void Main() { string str = "Hello World! Hello Universe! Hello!"; ...
Read MoreWhat is the best way to iterate over a Dictionary in C#?
A Dictionary is a collection of key-value pairs in C#. The Dictionary class is included in the System.Collections.Generic namespace and provides several efficient ways to iterate through its elements. There are multiple approaches to iterate over a Dictionary, each with its own advantages depending on whether you need keys, values, or both. Syntax Following are the common syntaxes for iterating over a Dictionary − // Using foreach with KeyValuePair foreach (KeyValuePair item in dictionary) { // Access item.Key and item.Value } // Using foreach with var foreach (var item in dictionary) ...
Read MoreWhat is static binding in C#?
Static binding in C# refers to the process of linking a function call to its implementation at compile time. This is also known as early binding or compile-time polymorphism. The compiler determines which method to call based on the method signature and the types of arguments passed. C# provides two main techniques to implement static binding through static polymorphism − Function Overloading − Multiple methods with the same name but different parameters Operator Overloading − Custom implementations for operators like +, -, *, etc. Static Binding Process ...
Read MoreC# Linq Sum() Method
The LINQ Sum() method in C# is used to calculate the sum of numeric values in a collection. It works with various numeric types including int, double, decimal, and float, and can also work with nullable types. Syntax Following is the basic syntax for the Sum() method − // For basic numeric collections collection.Sum() // With a selector function collection.Sum(selector) Parameters The Sum() method has the following parameter − selector (optional) − A function to extract numeric values from each element. Return Value Returns the sum of ...
Read MoreHow do we call a C# method recursively?
Recursion in C# is a programming technique where a method calls itself to solve a smaller version of the same problem. Each recursive call reduces the problem size until it reaches a base case that stops the recursion. A recursive method must have two essential components: a base case that stops the recursion, and a recursive case that calls the method with modified parameters. Syntax Following is the general syntax for a recursive method − public returnType MethodName(parameters) { if (baseCondition) { return baseValue; ...
Read MoreDateTimeOffset.Add() Method in C#
The DateTimeOffset.Add() method in C# returns a new DateTimeOffset object that represents the original date and time with a specified time interval added to it. This method is useful for performing date and time arithmetic while preserving the original offset information. Syntax Following is the syntax − public DateTimeOffset Add(TimeSpan timespan); Parameters timespan − A TimeSpan object that represents the time interval to add. This can be positive (to add time) or negative (to subtract time). Return Value Returns a new DateTimeOffset object whose value is the sum of ...
Read MoreC# program to convert binary string to Integer
Converting a binary string to an integer in C# can be accomplished using several methods. The most straightforward approach is using Convert.ToInt32() with base 2, but you can also implement manual conversion for better understanding of the binary-to-decimal process. Using Convert.ToInt32() Method The simplest way to convert a binary string to an integer is using the built-in Convert.ToInt32() method with base 2 − using System; class Program { static void Main() { string binaryStr = "1001"; ...
Read More