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
Programming Articles
Page 882 of 2547
How 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 MoreShort Time ("t") Format Specifier in C#
The Short Time format specifier ("t") in C# is a standard date and time format specifier that displays only the time portion of a DateTime in a short format. It excludes the date and seconds, showing only hours and minutes along with the AM/PM designator for 12-hour formats. The "t" format specifier is defined by the DateTimeFormatInfo.ShortTimePattern property of the current culture. Different cultures may display the time differently based on their regional settings. Syntax Following is the syntax for using the short time format specifier − DateTime.ToString("t") DateTime.ToString("t", CultureInfo) The underlying custom ...
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 MoreWhat are the different types of conditional statements supported by C#?
Conditional statements in C# allow programs to execute different code blocks based on specified conditions. These statements evaluate boolean expressions and direct program flow accordingly, enabling dynamic decision-making in applications. Types of Conditional Statements Statement Description if statement Executes a block of code when a boolean expression is true. if...else statement Executes one block if the condition is true, another if false. nested if statements Uses one if or else if statement inside another for complex conditions. switch statement Tests a variable against multiple values ...
Read MoreC# program to convert time from 12 hour to 24 hour format
Converting time from 12-hour format to 24-hour format in C# is a common requirement in applications. The DateTime.Parse() method can parse 12-hour time strings, and the ToString() method with the "HH:mm" format specifier converts it to 24-hour format. Syntax Following is the syntax for parsing 12-hour time and converting to 24-hour format − DateTime dateTime = DateTime.Parse("12-hour time string"); string time24Hour = dateTime.ToString("HH:mm"); The HH format specifier represents hours in 24-hour format (00-23), while mm represents minutes (00-59). Using DateTime.Parse() for Basic Conversion The simplest approach uses DateTime.Parse() to convert a 12-hour ...
Read MoreCollection Initialization in C#
Collection initialization in C# allows you to initialize collections with values at the time of creation using a concise syntax. This feature, introduced in C# 3.0, makes code more readable and eliminates the need for multiple Add() method calls. Collection initializers work with any collection type that implements IEnumerable and has an Add method, including List, Dictionary, arrays, and custom collections. Syntax Following is the syntax for collection initialization − CollectionType collectionName = new CollectionType { item1, item2, item3 }; For objects with properties, you can combine object and collection initialization − ...
Read MoreDecimal.ToOACurrency() Method in C#
The Decimal.ToOACurrency() method in C# converts a decimal value to an OLE Automation Currency value, which is represented as a 64-bit signed integer. OLE Automation Currency is a fixed-point data type that stores currency values with 4 decimal places of precision, making it suitable for financial calculations where precision is critical. The method multiplies the decimal value by 10, 000 to preserve 4 decimal places in the integer representation. This format is commonly used in COM interop scenarios and legacy systems that require OLE Automation compatibility. Syntax Following is the syntax for the Decimal.ToOACurrency() method − ...
Read MoreHow to use C# BinaryReader class?
The BinaryReader class in C# is used to read binary data from a stream in a specific encoding. It provides methods to read various data types like integers, doubles, strings, and byte arrays from binary files or streams. The BinaryReader class is located in the System.IO namespace and is commonly used for reading binary files, network streams, or any binary data format. Syntax Following is the syntax for creating a BinaryReader instance − BinaryReader reader = new BinaryReader(stream); Following is the syntax for reading different data types − int value = ...
Read MoreWhat is the difference between VAR and DYNAMIC keywords in C#?
The var and dynamic keywords in C# both allow you to declare variables without explicitly specifying their type, but they work very differently. The key difference is when type checking occurs — var is resolved at compile-time, while dynamic is resolved at runtime. Syntax Following is the syntax for declaring a var variable − var variableName = value; Following is the syntax for declaring a dynamic variable − dynamic variableName = value; Using var Keyword The var keyword creates statically typed variables. The compiler determines the type based on ...
Read MoreAction Delegate in C#
The Action delegate in C# is a built-in generic delegate type that represents a method with no return value (void). It can accept zero or more input parameters and is particularly useful for passing methods as parameters or storing method references. Syntax Following is the basic syntax for declaring an Action delegate − Action actionDelegate = MethodName; For Action delegates with parameters − Action actionDelegate = MethodName; Action actionDelegate = MethodName; Action actionDelegate = MethodName; Where T, T1, T2, etc. are the parameter types. Using Action Delegate with Single ...
Read More