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 Arjun Thakur
Page 9 of 75
C# Program to get free space in a drive
To get the free space in a drive, use the AvailableFreeSpace and TotalFreeSpace properties of the DriveInfo class in C#. This is useful for monitoring disk space and making decisions based on available storage. Syntax Following is the syntax for creating a DriveInfo object and accessing free space properties − DriveInfo driveInfo = new DriveInfo("DriveLetter"); long availableSpace = driveInfo.AvailableFreeSpace; long totalFreeSpace = driveInfo.TotalFreeSpace; Properties Property Description AvailableFreeSpace Gets the amount of available free space on a drive, in bytes, available to the current user. ...
Read MoreWhere do we use scope Resolution Operator (::) in C#?
The scope resolution operator (::) in C# is used to access types within namespaces, especially when dealing with namespace aliases or when there are naming conflicts. Unlike C++ where it's primarily used for global variables, in C# it specifically relates to namespace resolution. The scope resolution operator helps distinguish between types that share the same name but exist in different namespaces. It's particularly useful when using namespace aliases or the global keyword. Syntax Following is the syntax for using the scope resolution operator with namespace aliases − alias::TypeName Following is the syntax for ...
Read MoreWhat is the base class for all exceptions in C#?
In C#, the base class for all exceptions is System.Exception. This is the root class from which all exception types derive, providing a common structure and functionality for error handling throughout the .NET framework. The System.Exception class has two primary derived classes: System.SystemException for system-generated exceptions and System.ApplicationException for application-specific exceptions. However, Microsoft now recommends deriving custom exceptions directly from System.Exception or its appropriate subclasses rather than from System.ApplicationException. Exception Hierarchy C# Exception Hierarchy System.Exception System.SystemException ...
Read MoreGet all drives in C#
In C#, you can retrieve information about all drives on a system using the DriveInfo.GetDrives() method. This method returns an array of DriveInfo objects representing all logical drives on the current system, including hard drives, network drives, CD-ROMs, and other storage devices. Syntax Following is the syntax for getting all drives − DriveInfo[] drives = DriveInfo.GetDrives(); To iterate through the drives and access their properties − foreach (DriveInfo drive in drives) { Console.WriteLine(drive.Name); } Using GetDrives() to Display Drive Names The simplest approach is to get ...
Read MoreC# Program to read all the lines one by one in a file
The File.ReadAllLines() method in C# reads all lines from a file and returns them as a string array. This method is useful when you need to process a file line by line or access specific lines by their index. Syntax Following is the syntax for File.ReadAllLines() method − string[] lines = File.ReadAllLines(filePath); Parameters filePath − The path to the file to be read (string) Return Value Returns a string[] array containing all lines from the file. Each element represents one line from the file. Using ReadAllLines() to Read ...
Read MoreHow to select a random element from a C# list?
Selecting a random element from a C# list is a common task in programming. This involves using the Random class to generate a random index within the bounds of the list, then accessing the element at that index. Syntax Following is the basic syntax for selecting a random element from a list − Random random = new Random(); int index = random.Next(list.Count); var randomElement = list[index]; Using Random.Next() Method The Random.Next() method generates a random integer between 0 (inclusive) and the specified maximum value (exclusive). When you pass list.Count as the parameter, it ...
Read MoreWhat is the difference between String.Copy() and String.CopyTo() methods in C#?
The String.Copy() and String.CopyTo() methods in C# serve different purposes for copying string data. String.Copy() creates a new string object with the same content, while String.CopyTo() copies characters from a string into a character array. Syntax Following is the syntax for String.Copy() method − public static string Copy(string str) Following is the syntax for String.CopyTo() method − public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count) Parameters String.Copy() Parameters: str − The string to copy. String.CopyTo() Parameters: sourceIndex − The index of ...
Read MoreRepresent Int64 as a Octal string in C#
To represent Int64 as an octal string in C#, use the Convert.ToString() method and set the base as the second parameter to 8 for octal conversion. Int64 represents a 64-bit signed integer that can store values from -9, 223, 372, 036, 854, 775, 808 to 9, 223, 372, 036, 854, 775, 807. Syntax Following is the syntax for converting Int64 to octal string − Convert.ToString(longValue, 8) Parameters longValue − The Int64 value to convert 8 − The base for octal number system Return Value Returns a string representation ...
Read MoreUsing the new keyword in C#
The new keyword in C# is used to create instances of objects, arrays, and collections. It allocates memory for the object and calls the appropriate constructor to initialize it. Syntax Following is the syntax for using the new keyword to create objects − ClassName objectName = new ClassName(); Following is the syntax for creating arrays using new − dataType[] arrayName = new dataType[size]; Following is the syntax for creating collections using new − CollectionType collectionName = new CollectionType(); Using 'new' to Create Objects The most ...
Read MoreC# Program to display a string in reverse alphabetic order
To display a string in reverse order, you can convert the string to a character array and then use the Array.Reverse() method. This approach reverses the order of characters, displaying them from last to first. Syntax Following is the syntax for converting a string to character array − char[] arr = str.ToCharArray(); Following is the syntax for reversing the array − Array.Reverse(arr); Using Array.Reverse() Method The simplest approach is to convert the string to a character array and use Array.Reverse() to reverse the order of characters − ...
Read More