Arjun Thakur

Arjun Thakur

749 Articles Published

Articles by Arjun Thakur

Page 9 of 75

C# Program to get free space in a drive

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 773 Views

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 More

Where do we use scope Resolution Operator (::) in C#?

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 852 Views

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 More

What is the base class for all exceptions in C#?

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 2K+ Views

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 More

Get all drives in C#

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 742 Views

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 More

C# Program to read all the lines one by one in a file

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 448 Views

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 More

How to select a random element from a C# list?

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 45K+ Views

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 More

What is the difference between String.Copy() and String.CopyTo() methods in C#?

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 405 Views

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 More

Represent Int64 as a Octal string in C#

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 319 Views

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 More

Using the new keyword in C#

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 8K+ Views

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 More

C# Program to display a string in reverse alphabetic order

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 893 Views

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
Showing 81–90 of 749 articles
« Prev 1 7 8 9 10 11 75 Next »
Advertisements