Samual Sam

Samual Sam

1,507 Articles Published

Articles by Samual Sam

Page 7 of 151

C# Program to make a copy of an existing file

Samual Sam
Samual Sam
Updated on 17-Mar-2026 1K+ Views

The File.Copy method in C# is used to copy an existing file from one location to another. This method is part of the System.IO namespace and provides a simple way to duplicate files programmatically. Syntax Following is the syntax for the File.Copy method − File.Copy(string sourceFileName, string destFileName); File.Copy(string sourceFileName, string destFileName, bool overwrite); Parameters sourceFileName − The path of the file to copy. destFileName − The path of the destination file. overwrite − Optional boolean parameter. If true, overwrites the destination file if it already exists. ...

Read More

How to add integer values to a C# list?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 9K+ Views

To add integer values to a List in C#, you can use several methods including Add(), AddRange(), and collection initializer syntax. The Add() method is the most common way to add individual values one at a time. Syntax Following is the syntax for declaring an integer list and adding values − List listName = new List(); listName.Add(value); To add multiple values at once using AddRange() − listName.AddRange(new int[] { value1, value2, value3 }); Using collection initializer syntax − List listName = new List { value1, value2, value3 }; ...

Read More

Draw an ellipse in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 3K+ Views

To draw an ellipse in C#, you use the DrawEllipse() method from the Graphics class. This method requires a Pen object to define the ellipse's outline and either a Rectangle or coordinate parameters to specify the ellipse's position and size. Drawing ellipses is commonly done in Windows Forms applications using the Paint event handler or by overriding the OnPaint method. Syntax The DrawEllipse() method has several overloads − graphics.DrawEllipse(pen, rectangle); graphics.DrawEllipse(pen, x, y, width, height); Parameters pen − Defines the color, width, and style of the ellipse outline rectangle − Specifies ...

Read More

What is the Mutex class in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 2K+ Views

The Mutex class in C# is a synchronization primitive that provides mutually exclusive access to a shared resource. Unlike other synchronization mechanisms, Mutex can be used for both thread synchronization within a single process and interprocess synchronization across multiple processes. The name "Mutex" stands for mutual exclusion, ensuring that only one thread can access a protected resource at a time. Syntax Following are the common ways to create a Mutex instance − // Default constructor Mutex mutex = new Mutex(); // With initial ownership Mutex mutex = new Mutex(bool initiallyOwned); // With initial ...

Read More

How to pass pointers as parameters to methods in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 2K+ Views

To pass pointers as parameters to methods in C#, you need to use unsafe code and pointer syntax. Pointers allow direct memory manipulation and can be passed to methods for operations like swapping values or modifying data directly in memory. C# requires the unsafe keyword when working with pointers, and your project must be configured to allow unsafe code compilation. Syntax Following is the syntax for declaring a method that accepts pointer parameters − public unsafe void MethodName(int* pointer1, int* pointer2) { // pointer operations } Following is the ...

Read More

How to add read-only property in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 377 Views

A read-only property in C# is a property that can only be set during object initialization and cannot be modified afterward. There are several ways to create read-only properties, including using the readonly keyword with fields, properties with only getters, and init-only properties (available in C# 9.0+). Syntax Following is the syntax for a read-only field − readonly dataType fieldName; Following is the syntax for a read-only property with only a getter − public dataType PropertyName { get; } Following is the syntax for an init-only property (C# 9.0+) − ...

Read More

Check if a File exists in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 10K+ Views

The File.Exists() method in C# is used to check whether a file exists at a specified path. This method returns true if the file exists, and false if it does not. It is part of the System.IO namespace and provides a simple way to verify file existence before performing file operations. Syntax Following is the syntax for the File.Exists() method − public static bool Exists(string path) Parameters path − The file path to check. Can be a relative path (current directory) or absolute path (full path including drive). Return Value ...

Read More

What are the main parts of a C# program?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 2K+ Views

The main parts of a C# program include − Namespace declaration A class Class methods Class attributes A Main method Statements and Expressions Comments Understanding these components is essential for writing effective C# programs. Each part serves a specific purpose in organizing and executing your code. Basic C# Program Structure The following example demonstrates a simple C# program with all the essential parts − using System; namespace Demo { class Program { // Class attribute (field) ...

Read More

Sorting a String in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 7K+ Views

Sorting a string array in C# is accomplished using the Array.Sort() method, which arranges elements in alphabetical order. This method modifies the original array and uses lexicographic comparison by default. Syntax Following is the basic syntax for sorting a string array − Array.Sort(arrayName); For custom sorting, you can use an overloaded version − Array.Sort(arrayName, StringComparer.OrdinalIgnoreCase); Using Array.Sort() for String Arrays The Array.Sort() method sorts string arrays in ascending alphabetical order. Here's how it works − using System; public class Program { public static ...

Read More

Convert.ToSingle Method in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 2K+ Views

The Convert.ToSingle() method in C# converts a specified value to a single-precision floating-point number (float). This method can convert various data types including boolean, integer, string, and other numeric types to a 32-bit floating-point representation. Syntax Following is the syntax for the Convert.ToSingle() method − public static float ToSingle(object value); public static float ToSingle(bool value); public static float ToSingle(int value); public static float ToSingle(string value); Parameters value − The value to be converted to a single-precision floating-point number. Can be of various types including bool, int, string, double, decimal, etc. ...

Read More
Showing 61–70 of 1,507 articles
« Prev 1 5 6 7 8 9 151 Next »
Advertisements