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 65 of 196
How to write Regex for numbers only in C#?
Regular expressions (regex) are patterns used to match specific text formats. In C#, you can create regex patterns to validate that input contains only numbers. The System.Text.RegularExpressions.Regex class provides methods to test strings against these patterns. For numbers-only validation, you need to understand the key regex metacharacters and how to construct patterns that accept different numeric formats like integers, decimals, and negative numbers. Syntax Basic regex pattern for numbers only − Regex regex = new Regex(@"^[0-9]+$"); Pattern for numbers with optional decimal point − Regex regex = new Regex(@"^[0-9]+\.?[0-9]*$"); ...
Read MoreHow do you get the file size in C#?
Getting the file size in C# can be accomplished using several methods. The most common approach is using the FileInfo class, which provides the Length property to retrieve the size of a file in bytes. You can also use static methods from the File class for quick operations without creating instances. The FileInfo class is part of the System.IO namespace and provides comprehensive file manipulation capabilities including size retrieval, creation dates, and access permissions. Syntax Using FileInfo class to get file size − FileInfo fileInfo = new FileInfo(filePath); long fileSize = fileInfo.Length; Using ...
Read MoreHow to check whether you are connected to Internet or not in C#?
There are several ways to check whether a machine is connected to the internet in C#. The most common approach uses the System.Net namespace, which provides methods for sending and receiving data from resources identified by a URI. The WebClient or HttpClient classes are particularly useful for this purpose. The technique involves making a request to a reliable URL and checking if the response is successful. Google's http://google.com/generate_204 endpoint is commonly used because it returns a minimal response, making it efficient for connectivity checks. Using WebClient for Internet Connectivity Check Example using System; using System.Net; ...
Read MoreHow do I overload the [] operator in C#?
The indexer ([] operator) in C# allows an object to be accessed like an array using square bracket notation. When you define an indexer for a class, instances of that class behave like virtual arrays, enabling array-style access using the [] operator. Indexers can be overloaded with different parameter types and counts. The parameters don't have to be integers — you can use strings, multiple parameters, or any other type as the index. Syntax Following is the basic syntax for defining an indexer − public returnType this[indexType index] { get { ...
Read MoreHow to Deserializing JSON to .NET object using Newtonsoft json in C# and pick only one value from the array?
JSON deserialization is the process of converting JSON data into .NET objects. The Newtonsoft.Json library (also known as Json.NET) provides powerful methods to deserialize JSON strings into strongly-typed C# objects. When working with JSON arrays, you can easily pick specific values from the deserialized array. Syntax Following is the syntax for deserializing JSON to a .NET object − T obj = JsonConvert.DeserializeObject(jsonString); For deserializing JSON arrays − T[] objArray = JsonConvert.DeserializeObject(jsonString); Using WebClient to Fetch and Deserialize JSON The WebClient class provides methods for downloading data from web resources. ...
Read MoreHow to use "not in" query with C# LINQ?
In C# LINQ, there are several ways to perform "not in" queries to exclude items from one collection that exist in another. The most common approaches use the Except operator, the Where clause with Any, or the Contains method. These operators work with any data source that implements IEnumerable, making them versatile for querying collections, arrays, and LINQ query results. Syntax Using the Except operator − var result = collection1.Except(collection2); Using Where with Any − var result = from item in collection1 ...
Read MoreHow to set a property value by reflection in C#?
The System.Reflection namespace in C# provides classes that allow you to obtain information about applications and dynamically add types, values, and objects at runtime. One of the most common uses of reflection is setting property values dynamically when you don't know the property name at compile time. Reflection allows you to examine various types in an assembly, instantiate these types, perform late binding to methods and properties, and even create new types at runtime. Syntax Following is the syntax for setting a property value using reflection − Type type = obj.GetType(); PropertyInfo property = type.GetProperty("PropertyName"); ...
Read MoreHow to convert string to title case in C#?
Title case is any text, such as in a title or heading, where the first letter of major words is capitalized. Title case or headline case is a style of capitalization used for rendering the titles of published works or works of art in English. When using title case, all words are capitalized except for minor words (like articles, prepositions, and conjunctions) unless they are the first or last word of the title. C# provides the ToTitleCase method through the TextInfo class to convert strings to title case format. This method capitalizes the first letter of each word while ...
Read MoreHow to use String Format to show decimal up to 2 places or simple integer in C#?
The String.Format method in C# converts values to formatted strings and inserts them into another string. It's particularly useful for displaying numbers with consistent decimal places, showing integers as decimals when needed, or keeping integers without unnecessary decimal points. Syntax Following is the syntax for String.Format with decimal formatting − string.Format("{0:format}", value) Common decimal format specifiers − "{0:0.00}" // Always shows 2 decimal places "{0:F2}" // Fixed-point with 2 decimal places "{0:N2}" // Number format with 2 decimal places ...
Read MoreWhat are the benefits to marking a field as readonly in C#?
The readonly keyword in C# is used to declare a member variable as constant, but allows the value to be calculated at runtime. This differs from a constant declared with the const modifier, which must have its value set at compile time. Using readonly you can set the value of the field either in the declaration, or in the constructor of the object that the field is a member of. The readonly modifier provides several benefits including immutability after initialization, runtime value assignment, and thread safety for the field once set. Syntax Following is the syntax for ...
Read More