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
Articles by Chandu yadav
Page 10 of 81
How to generate the first 100 Odd Numbers using C#?
To generate the first 100 odd numbers in C#, you can use different approaches. The most straightforward method is to iterate through numbers and check if they are odd using the modulus operator. Syntax Following is the syntax for checking if a number is odd − if (number % 2 != 0) { // number is odd } Following is the syntax for generating odd numbers in a loop − for (int i = 1; i
Read MoreC# Linq LastorDefault Method
The LastOrDefault() method in C# LINQ returns the last element of a sequence, or a default value if the sequence is empty. This method prevents exceptions that would occur with Last() when called on empty collections. Syntax Following is the syntax for LastOrDefault() method − public static T LastOrDefault(this IEnumerable source); public static T LastOrDefault(this IEnumerable source, Func predicate); Parameters source − The sequence to return the last element from. predicate − A function to test each element for a condition (optional). Return Value Returns the last element that ...
Read MoreDifferent ways for Integer to String Conversions in C#
Converting integers to strings is a common operation in C# programming. There are several built-in methods available, each with different use cases and performance characteristics. Syntax Following are the main syntaxes for integer to string conversion − // Using ToString() method string str = number.ToString(); // Using Convert.ToString() method string str = Convert.ToString(number); // Using string interpolation string str = $"{number}"; // Using string.Format() method string str = string.Format("{0}", number); Using ToString() Method The ToString() method is the most commonly used approach for converting integers to strings. It's called directly ...
Read MoreC# Program to split a string on spaces
In C#, the Split() method is used to divide a string into an array of substrings based on a specified delimiter. When splitting a string on spaces, we use the space character ' ' as the delimiter. Syntax Following is the syntax for splitting a string on spaces − string[] result = str.Split(' '); You can also use the overloaded version with options − string[] result = str.Split(' ', StringSplitOptions.RemoveEmptyEntries); Parameters separator − The character used to split the string (space character ' ' in this case). ...
Read MoreC# Program to convert a Byte value to an Int32 value
To convert a byte value to an int value in C#, you can use the Convert.ToInt32() method or implicit conversion. Since a byte (8-bit unsigned integer) can always fit within an int (32-bit signed integer), the conversion is straightforward and safe. An Int32 represents a 32-bit signed integer that can store values from -2, 147, 483, 648 to 2, 147, 483, 647, while a byte stores values from 0 to 255. Syntax Using Convert.ToInt32() method − int result = Convert.ToInt32(byteValue); Using implicit conversion − int result = byteValue; Using ...
Read MoreWhat are contextual keywords in C#?
In C#, contextual keywords are special identifiers that have reserved meaning only in specific contexts. Unlike regular keywords, they can still be used as variable names or identifiers when not in their special context. Common examples include get and set in properties, where in LINQ queries, and partial in class definitions. Contextual keywords provide flexibility by allowing the same word to serve as both a keyword and an identifier depending on the context in which it appears. Syntax Contextual keywords are used within their specific contexts. Here are some common patterns − // Property accessors ...
Read MoreWhat are the hidden features of C#?
C# contains several powerful but often overlooked features that can significantly improve code readability, safety, and efficiency. These hidden gems can make your C# programming more elegant and productive when used appropriately. Lambda Expressions A lambda expression is a concise way to write anonymous functions using the => operator (called the "goes to" operator). Lambda expressions are commonly used with LINQ operations and delegate assignments. Syntax (parameters) => expression (parameters) => { statements; } Example using System; using System.Linq; class Program { public static void Main() ...
Read MoreSet 6-item tuple in C#'
A 6-item tuple in C# is a data structure that can hold six values of different or same data types. Tuples are useful when you need to return multiple values from a method or group related data together without creating a separate class. Syntax Following is the syntax for creating a 6-item tuple using the Tuple class − var tuple = new Tuple(item1, item2, item3, item4, item5, item6); Alternatively, you can use the modern tuple syntax with parentheses − var tuple = (item1, item2, item3, item4, item5, item6); Using Traditional ...
Read MoreInbuilt Data Structures in C#
C# provides several powerful built-in data structures that make it easier to store, organize, and manipulate collections of data. These data structures are part of the .NET Framework and offer different capabilities for various programming scenarios. The most commonly used built-in data structures include List for dynamic arrays, ArrayList for non-generic collections, Dictionary for key-value pairs, and Queue and Stack for specialized ordering operations. List The generic List is a strongly-typed collection that can dynamically resize itself. Unlike arrays, you don't need to specify the size at compile time, and it provides better type safety compared to ...
Read MoreC# Linq Contains Method
The Contains() method in LINQ is used to check whether a sequence contains a specific element. It returns true if the element is found, otherwise false. This method works with any IEnumerable collection including arrays, lists, and queryable sequences. Syntax Following is the syntax for using Contains() with collections − bool result = collection.Contains(element); Following is the syntax for using Contains() with queryable sequences − bool result = collection.AsQueryable().Contains(element); Parameters element − The value to locate in the sequence. Return Value Returns true if the ...
Read More