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 103 of 2547
Write a C# program to check if a number is prime or not
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. To check if a number is prime in C#, we count how many divisors it has by iterating through all numbers from 1 to the number itself. The basic approach uses a counter that increments each time we find a divisor. If the counter equals 2 at the end (divisible only by 1 and itself), the number is prime. Algorithm The algorithm for checking prime numbers follows these steps − Initialize a counter to 0 ...
Read MoreInteger literals vs Floating point literals in C#
In C#, literals are fixed values written directly in the source code. There are two main categories of numeric literals: integer literals for whole numbers and floating-point literals for decimal numbers. Understanding the difference between these literals is essential for proper variable declaration and avoiding compilation errors. Integer Literals An integer literal represents a whole number without a decimal point. Integer literals can be decimal (base 10) or hexadecimal (base 16). A prefix specifies the base: 0x or 0X for hexadecimal, with no prefix for decimal. Syntax decimal_literal // 10, 100, ...
Read Morefinal, finally and finalize in C#
In C#, the terms final, finally, and finalize serve different purposes. While final doesn't exist in C#, finally and finalize are crucial concepts for exception handling and resource cleanup respectively. final (sealed in C#) C# does not have a final keyword like Java. Instead, C# uses the sealed keyword to achieve similar functionality − public sealed class SealedClass { // cannot be inherited } public override sealed void SealedMethod() { // cannot be overridden further } The sealed keyword prevents overriding of methods or inheritance of classes. ...
Read MoreFind free disk space using C#
The DriveInfo class in C# provides information about drives and their storage capacity. You can use this class to find free disk space, total disk space, and calculate the percentage of available space on any drive. Syntax Following is the syntax for creating a DriveInfo instance − DriveInfo driveInfo = new DriveInfo("driveLetter"); Following are the key properties for disk space information − long availableSpace = driveInfo.AvailableFreeSpace; long totalSpace = driveInfo.TotalSize; long usedSpace = driveInfo.TotalSize - driveInfo.AvailableFreeSpace; Using DriveInfo to Get Free Disk Space Example using System; using ...
Read MoreDefault value of bool in C#
The default operator in C# returns the default value for any data type. For the bool type, the default value is false. When a bool variable is declared without initialization, it automatically gets assigned the default value of false. Syntax Following is the syntax for using the default operator with bool − bool variable = default(bool); In C# 7.1 and later, you can also use the simplified syntax − bool variable = default; Using default(bool) Operator The following example demonstrates how to get the default value of bool ...
Read MoreDictionary.ContainsValue() Method in C#
The Dictionary.ContainsValue() method in C# is used to check whether the Dictionary contains a specific value or not. This method returns true if the value is found in the dictionary, otherwise it returns false. Syntax public bool ContainsValue(TValue val); Parameters val − The value to search for in the dictionary. This parameter can be null if the value type allows null values. Return Value Returns true if the dictionary contains the specified value; otherwise, false. Using ContainsValue() with Found Value The following example demonstrates checking for a value that exists ...
Read MoreUInt32.ToString() Method in C# with Examples
The UInt32.ToString() method in C# is used to convert the numeric value of a UInt32 instance to its equivalent string representation. This method is particularly useful when you need to display unsigned 32-bit integer values as text or concatenate them with strings. The UInt32 type represents unsigned 32-bit integers with values ranging from 0 to 4, 294, 967, 295. The ToString() method provides several overloads to format the output according to different requirements. Syntax Following are the main syntax variations of the UInt32.ToString() method − public override string ToString(); public string ToString(string format); public string ...
Read MoreHow to use the sleep method in C#?
The Thread.Sleep() method in C# is used to pause the execution of the current thread for a specified period of time. This is commonly used to introduce delays in program execution or to simulate time-consuming operations. Syntax The Thread.Sleep() method has two overloads − Thread.Sleep(int millisecondsTimeout); Thread.Sleep(TimeSpan timeout); Parameters millisecondsTimeout − The number of milliseconds for which the thread is suspended. Use Timeout.Infinite (-1) to suspend the thread indefinitely. timeout − A TimeSpan that sets the amount of time for which the thread is suspended. Using Thread.Sleep() with Milliseconds ...
Read MoreHow to check if a string is a valid keyword in C#?
To check if a string is a valid keyword in C#, you can use the IsValidIdentifier method from the CodeDomProvider class. This method determines whether a given string is a valid identifier or a reserved keyword in C#. The IsValidIdentifier method returns true if the string is a valid identifier (like variable names, method names), and false if it's a reserved keyword (like for, if, class, etc.). Syntax Following is the syntax for using IsValidIdentifier method − CodeDomProvider provider = CodeDomProvider.CreateProvider("C#"); bool isIdentifier = provider.IsValidIdentifier(stringToCheck); Using IsValidIdentifier to Check Keywords Here's how ...
Read MoreThe nameof keyword in C#
The nameof operator in C# returns the string literal name of a variable, type, or member. It provides a compile-time constant string that represents the name of the code element, making it useful for logging, exception messages, and property change notifications without hardcoding strings. Syntax Following is the syntax for using the nameof operator − string name = nameof(element); Where element can be a variable, property, method, class, or namespace. Using nameof with Variables The nameof operator returns the variable name as a string, which is resolved at compile time − ...
Read More