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 26 of 143
Date Class in C#
The DateTime class in C# is used to work with dates and times. It represents an instant in time, ranging from 12:00:00 midnight, January 1, 0001 to 11:59:59 P.M., December 31, 9999 A.D. The DateTime class provides various methods and properties to create, manipulate, and format date and time values. Syntax Following is the syntax for creating a DateTime object − DateTime variableName = new DateTime(year, month, day); DateTime variableName = new DateTime(year, month, day, hour, minute, second); To get the current date and time − DateTime current = DateTime.Now; ...
Read MoreReturn a C# tuple from a method
A tuple in C# is a data structure that can hold multiple values of different types. You can return a tuple from a method, which is useful when you need to return multiple values from a single method call. There are two main ways to return tuples from methods in C# − using the Tuple class and using the newer value tuples with more concise syntax. Syntax Using the Tuple class − static Tuple MethodName() { return Tuple.Create(value1, value2, value3); } Using value tuples (C# 7.0 and later) − ...
Read MoreWhat are postfix operators in C#?
The postfix operators in C# are the increment (++) and decrement (--) operators that are placed after the variable. With postfix operators, the current value of the variable is returned first, and then the variable is incremented or decremented by 1. Syntax Following is the syntax for postfix increment and decrement operators − variable++; // postfix increment variable--; // postfix decrement How Postfix Operators Work The key behavior of postfix operators is that they return the original value before performing the increment or decrement operation. This is different from prefix operators ...
Read MoreWhat does the interface IList do in C#?
The IList interface in C# represents a non-generic collection of objects that can be individually accessed by index. It provides indexed access to elements, allowing you to retrieve, modify, add, and remove items at specific positions within the collection. The IList interface combines the functionality of ICollection and IEnumerable, making it suitable for collections that need both indexed access and dynamic resizing capabilities. Syntax Following is the syntax for declaring an IList variable − IList list = new ArrayList(); IList list = new List(); Following is the syntax for accessing elements by index ...
Read MoreReturn 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 More