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 51 of 2547
What is an Optional parameter in C#?
By default, all parameters of a method are required. However, C# allows you to define optional parameters that do not force you to pass arguments at calling time. This means you can call a method without passing values for some parameters. Optional parameters contain default values in the function definition. If you do not pass an argument for an optional parameter at calling time, the default value is used automatically. There are different ways to make a parameter optional in C#. Syntax Following is the syntax for declaring optional parameters using default values − ...
Read MoreHow to download a file from a URL in C#?
A file can be downloaded from a URL using WebClient, which is available in the System.Net namespace. The WebClient class provides common methods for sending data to or receiving data from any local, intranet, or Internet resource identified by a URI. The WebClient class uses the WebRequest class internally to provide access to web resources. It offers a simple, high-level interface for downloading files without dealing with complex HTTP protocols directly. Syntax Following is the syntax for downloading a file using WebClient.DownloadFile method − WebClient client = new WebClient(); client.DownloadFile("sourceUrl", "destinationPath"); Parameters ...
Read MoreHow to change the WindowTop of the Console in C#?
The Console.WindowTop property in C# gets or sets the top position of the console window relative to the screen buffer. This property is useful when you need to programmatically control the console window's vertical position within the buffer area. Syntax Following is the syntax to get or set the WindowTop property − // Get the current WindowTop position int topPosition = Console.WindowTop; // Set the WindowTop position Console.WindowTop = value; Parameters The WindowTop property accepts an integer value representing the top row of the console window area within the screen buffer. The ...
Read MoreGetting an enumerator that iterates through HashSet in C#
A HashSet in C# is a collection that stores unique elements without duplicates. To iterate through a HashSet, you can use the GetEnumerator() method which returns an enumerator object, or use a foreach loop for simpler iteration. The HashSet.Enumerator provides manual control over iteration using MoveNext() and Current properties, while foreach handles enumeration automatically behind the scenes. Syntax Following is the syntax for getting an enumerator from a HashSet − HashSet.Enumerator enumerator = hashSet.GetEnumerator(); while (enumerator.MoveNext()) { T current = enumerator.Current; // use current element } ...
Read MoreCheck whether the Dictionary contains a specific value or not in C#
The Dictionary class in C# provides the ContainsValue() method to check whether a specific value exists in the dictionary. This method returns true if the value is found, otherwise false. Syntax Following is the syntax for the ContainsValue() method − public bool ContainsValue(TValue value); Parameters value − The value to locate in the Dictionary. Return Value Returns true if the Dictionary contains an element with the specified value; otherwise, false. Using ContainsValue() - Value Found The following example demonstrates checking for an existing value in ...
Read MoreHow to make a method deprecated in C#?
The Obsolete Attribute marks elements like classes, methods, properties, fields, delegates, and many others within our code as deprecated or obsolete. The attribute is read at compile time and is used to generate a warning or an error to the developer. This attribute can help if we have ever wanted to make sure programmers use newer versions of methods. It also makes it easier when we are transitioning from older methods to newer ones. Marking an item as obsolete warns users that program elements will be removed in future versions of the code base. This attribute is found ...
Read MoreWhat is Interface segregation principle and how to implement it in C#?
The Interface Segregation Principle (ISP) is the fourth principle of SOLID design principles. It states that clients should not be forced to depend upon interfaces that they don't use. Instead of creating one large interface with many methods, it's better to create multiple smaller, focused interfaces that serve specific purposes. This principle promotes high cohesion and loose coupling by ensuring that classes only implement the methods they actually need, making the code more maintainable and flexible. Syntax Following is the syntax for creating segregated interfaces − public interface ISpecificInterface { // Only ...
Read MoreSortedDictionary.Values Property in C#
The SortedDictionary.Values property in C# is used to get a collection containing the values in the SortedDictionary. This property returns a ValueCollection that preserves the sorted order of the original dictionary based on the keys. Syntax Following is the syntax for the Values property − public SortedDictionary.ValueCollection Values { get; } Return Value The property returns a SortedDictionary.ValueCollection containing all the values in the sorted dictionary. The values maintain the same order as their corresponding keys in the sorted dictionary. Using Values Property to Iterate Through Values The following example demonstrates ...
Read MoreCheck if ListDictionary contains a specific key in C#
The ListDictionary class in C# provides the Contains() method to check if a specific key exists in the collection. This method returns true if the key is found, otherwise false. ListDictionary is part of the System.Collections.Specialized namespace and is optimized for small collections. Syntax Following is the syntax for using the Contains() − public bool Contains(object key); Parameters key − The object to locate in the ListDictionary. Return Value Returns true if the ListDictionary contains an element with the specified key; otherwise, false. Using Contains() Method ...
Read MoreGet the TypeCode for value type UInt64 in C#
The GetTypeCode() method in C# returns a TypeCode enumeration value that identifies the specific type of a variable. For UInt64 (unsigned 64-bit integer) values, this method always returns TypeCode.UInt64, regardless of the actual numeric value stored. Syntax Following is the syntax for getting the TypeCode of a UInt64 value − TypeCode typeCode = uintValue.GetTypeCode(); Return Value The method returns TypeCode.UInt64 for all ulong variables, which represents the 64-bit unsigned integer type in the .NET type system. Using GetTypeCode() with UInt64 Values Example using System; public class Demo { ...
Read More