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 31 of 142
Return the total elements in a sequence as a 64-bit signed integer in C#
The LongCount() method in C# returns the total number of elements in a sequence as a 64-bit signed integer (long). This method is particularly useful when dealing with large datasets where the element count might exceed the range of a regular 32-bit integer. The LongCount() method is available in both LINQ to Objects and LINQ to Entities, and can be used with any IEnumerable or IQueryable collection. Syntax Following is the syntax for using LongCount() method − public static long LongCount(this IEnumerable source) public static long LongCount(this IEnumerable source, Func predicate) Parameters ...
Read MoreCovariance and Contravariance in C#
Covariance and contravariance in C# enable flexible type relationships when working with generics, delegates, and interfaces. Covariance allows you to use a more derived type than originally specified, while contravariance allows you to use a more general type than originally specified. These concepts are essential for understanding how type safety works with generic interfaces and delegates, particularly when dealing with inheritance hierarchies. Class Hierarchy Example Let us consider the following class hierarchy where One is the base class, Two inherits from One, and Three inherits from Two − using System; class One { ...
Read MoreC# program to print all distinct elements of a given integer array in C#
Finding distinct elements in an array is a common programming task in C#. There are several approaches to accomplish this, including using Dictionary, HashSet, and LINQ methods. Each approach has its own advantages depending on your specific requirements. Using Dictionary to Count Occurrences A Dictionary allows us to store each element as a key and its occurrence count as the value. This approach is useful when you need both distinct elements and their frequencies − using System; using System.Collections.Generic; class Program { public static void Main() { ...
Read MoreObject Initializer in C#
An object initializer in C# allows you to initialize an object's properties or fields at the time of object creation without explicitly calling a constructor with parameters. This feature provides a more readable and concise way to create and initialize objects. Object initializers use curly braces {} to assign values to accessible properties or fields immediately after creating the object instance. Syntax Following is the basic syntax for object initializers − ClassName objectName = new ClassName() { PropertyName1 = value1, PropertyName2 = value2, ...
Read MoreHow to compile unsafe code in C#?
Unsafe code in C# allows direct memory manipulation using pointers, which bypasses the .NET garbage collector's safety mechanisms. To compile unsafe code, you need to enable unsafe context compilation through specific compiler settings. Command-Line Compilation For compiling unsafe code using the command-line compiler, you must specify the /unsafe switch − csc /unsafe filename.cs For example, to compile a program named one.cs containing unsafe code − csc /unsafe one.cs Visual Studio IDE Configuration In Visual Studio, you need to enable unsafe code compilation in the project properties. Follow these steps ...
Read MoreC# program to accept two integers and return the remainder
The remainder operation in C# finds the leftover value after dividing one integer by another. The modulus operator (%) is used to calculate the remainder when the first number is divided by the second number. Syntax Following is the syntax for calculating remainder using the modulus operator − int remainder = dividend % divisor; Where dividend is the number being divided and divisor is the number by which we divide. Using Basic Modulus Operation The simplest way to find the remainder is using the modulus operator directly − using System; ...
Read MoreAccess Modifiers in C#
Access modifiers in C# control the visibility and accessibility of classes, methods, properties, and other members. They define which parts of your code can access specific members, providing encapsulation and security to your applications. Access Modifiers Overview C# provides five access modifiers that determine the scope and accessibility of class members − C# Access Modifiers Hierarchy public Most accessible protected internal internal protected private ...
Read MoreBeginning C# programming with Hello World
The following is a simple "Hello World" program in C# programming. This is typically the first program you write when learning a new language, and it demonstrates the basic structure of a C# application. Syntax Following is the basic structure of a C# program − using System; namespace NamespaceName { class ClassName { static void Main(string[] args) { // Your code here } } } Hello ...
Read MoreAbstract Classes in C#
An abstract class in C# includes both abstract and non-abstract methods. A class is declared abstract using the abstract keyword. You cannot instantiate an abstract class directly — it must be inherited by a derived class that provides implementations for all abstract methods. Syntax Following is the syntax for declaring an abstract class and an abstract method − public abstract class ClassName { // Abstract method — no body, must be overridden public abstract void MethodName(); // Non-abstract method — has a body, can be inherited ...
Read More'this' keyword in C#
The this keyword in C# is used to refer to the current instance of the class. It is also used to differentiate between method parameters and class fields when they have the same name. Another usage of the this keyword is to call another constructor from a constructor in the same class, known as constructor chaining. Syntax Following is the syntax for using this to refer to instance members − this.fieldName = value; Following is the syntax for constructor chaining using this − public ClassName(int a) : this(a, 0) { ...
Read More