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 karthikeya Boyini
Page 6 of 143
Why do we use internal keyword in C#?
The internal keyword in C# is an access modifier that provides assembly-level access. It allows class members to be accessible within the same assembly but restricts access from other assemblies, making it ideal for creating APIs that should only be used internally within your project. Syntax Following is the syntax for declaring internal members − internal class ClassName { // class accessible within assembly only } class MyClass { internal int field; // field accessible within assembly ...
Read MoreGroupBy() Method in C#
The GroupBy() method in C# is a LINQ extension method that groups elements from a collection based on a specified key selector function. It returns an IGrouping where elements sharing the same key are grouped together. Syntax Following is the syntax for the GroupBy() method − public static IEnumerable GroupBy( this IEnumerable source, Func keySelector ) Parameters source − The collection to group elements from. keySelector − A function that extracts the key for each element. Return Value Returns an ...
Read MoreRead in a file in C# with StreamReader
The StreamReader class in C# is used to read text files efficiently. It provides methods to read characters, lines, or the entire content of a text file. StreamReader is part of the System.IO namespace and implements IDisposable, making it suitable for use with using statements for automatic resource cleanup. Syntax Following is the syntax to create a StreamReader object − StreamReader sr = new StreamReader("filename.txt"); Following is the syntax to use StreamReader with automatic disposal − using (StreamReader sr = new StreamReader("filename.txt")) { // read operations } ...
Read MoreDifference between TimeSpan Seconds() and TotalSeconds()
The TimeSpan.Seconds property returns only the seconds component of a time span, whereas TimeSpan.TotalSeconds property converts the entire time duration into seconds. Understanding this difference is crucial when working with time calculations in C#. Syntax Following is the syntax for accessing the seconds component − TimeSpan ts = new TimeSpan(days, hours, minutes, seconds, milliseconds); int seconds = ts.Seconds; Following is the syntax for getting total seconds − TimeSpan ts = new TimeSpan(days, hours, minutes, seconds, milliseconds); double totalSeconds = ts.TotalSeconds; How It Works TimeSpan: 1 ...
Read MoreC# program to find the most frequent element
Finding the most frequent element in a string is a common programming problem that involves counting the occurrences of each character and determining which appears most often. In C#, this can be efficiently solved using an array to track character frequencies. Syntax Following is the basic approach to count character frequencies − int[] frequency = new int[256]; // ASCII character set for (int i = 0; i < str.Length; i++) { frequency[str[i]]++; } Using Array-Based Frequency Counting The most straightforward approach uses an integer array where each index corresponds ...
Read MoreWrite a C# program to calculate a factorial using recursion
Factorial of a number is the product of all positive integers less than or equal to that number. For example, 5! = 5 × 4 × 3 × 2 × 1 = 120. We can calculate factorial using recursion, where a function calls itself with a smaller value until it reaches the base case. Syntax Following is the syntax for a recursive factorial function − public int Factorial(int n) { if (n == 0 || n == 1) return 1; else ...
Read MoreC# program to return an array from methods
In C#, methods can return arrays by specifying the array type in the method signature and using the return statement. This allows you to create, populate, and return arrays from methods to be used by the calling code. Syntax Following is the syntax for declaring a method that returns an array − dataType[] MethodName() { // create and populate array return arrayVariable; } To call the method and use the returned array − dataType[] result = MethodName(); Returning String Arrays from Methods ...
Read MoreC# program to get the last element from an array
In C#, there are several ways to get the last element from an array. The most common approach is to use the array's Length property to access the element at the last index position. Syntax Following is the syntax to get the last element using array indexing − arrayName[arrayName.Length - 1] You can also use LINQ methods to get the last element − arrayName.Last() arrayName.LastOrDefault() Using Array Indexing The most efficient way to get the last element is by using the array's length minus one as the index − ...
Read MoreC# Program to merge sequences
In C#, you can merge sequences using the Zip method from LINQ. The Zip method combines elements from two sequences by pairing them together using a specified function. Syntax The Zip method syntax is − sequence1.Zip(sequence2, (first, second) => result) Parameters sequence1 − The first sequence to merge sequence2 − The second sequence to merge resultSelector − A function that defines how to combine elements from both sequences How It Works The Zip method pairs elements from two sequences by their index position. If sequences have different lengths, the ...
Read MoreHow to find the Sum of two Binary Numbers using C#?
Adding two binary numbers in C# involves performing binary arithmetic bit by bit, just like manual addition but working in base-2. The process includes handling carry operations when the sum of bits exceeds 1. Binary addition follows simple rules: 0 + 0 = 0, 0 + 1 = 1, 1 + 0 = 1, and 1 + 1 = 10 (which means 0 with a carry of 1). Syntax Following is the basic approach for binary addition − // Extract rightmost bits bit1 = val1 % 10; bit2 = val2 % 10; // Calculate ...
Read More