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 Samual Sam
Page 27 of 151
What is a copy constructor in C#?
A copy constructor in C# creates a new object by copying variables from another object of the same class. It provides a way to initialize a new object with the values of an existing object, creating a separate copy rather than a reference to the original object. Syntax Following is the syntax for creating a copy constructor − public ClassName(ClassName obj) { this.field1 = obj.field1; this.field2 = obj.field2; // copy other fields } How Copy Constructor Works A copy constructor takes an object of ...
Read MoreConvert.ToBoolean Method in C#
The Convert.ToBoolean method in C# converts a specified value to an equivalent Boolean value. This method follows specific rules depending on the input type − numeric types return false if zero, true otherwise; string values are parsed based on their content. Syntax Following is the syntax for the Convert.ToBoolean method − public static bool ToBoolean(object value); public static bool ToBoolean(string value); public static bool ToBoolean(int value); public static bool ToBoolean(double value); // Other overloads for different data types Parameters value − The value to convert to a Boolean. Can be a ...
Read MoreWhat is finally statement in C#?
The finally statement in C# is used to execute a given set of statements, whether an exception is thrown or not thrown. For example, if you open a file, it must be closed whether an exception is raised or not. The error handling blocks are implemented using the try, catch, and finally keywords. The finally block is guaranteed to execute, making it ideal for cleanup operations like closing files, database connections, or releasing resources. Syntax Following is the syntax for the finally statement − try { // code that might throw an ...
Read MoreC# Program to create a Simple Thread
In C#, a thread represents a separate execution path that can run concurrently with other threads. The System.Threading.Thread class allows you to create and manage threads in your application. To create a simple thread, you define a method that contains the code you want to execute, then create a Thread object using a ThreadStart delegate that points to your method. Syntax Following is the syntax for creating a thread − Thread threadName = new Thread(new ThreadStart(methodName)); threadName.Start(); You can also use the simplified syntax − Thread threadName = new Thread(methodName); threadName.Start(); ...
Read MoreWhat is overloading in C#?
C# provides two techniques to implement static polymorphism − Function overloading Operator overloading Function Overloading Function overloading in C# allows multiple methods with the same name but different parameters within the same class. The compiler determines which method to call based on the number, types, and order of arguments passed. Syntax Following is the syntax for function overloading − public returnType MethodName(parameter1) { } public returnType MethodName(parameter1, parameter2) { } public returnType MethodName(differentType parameter) { } Method Overloading Resolution Add(int, ...
Read MoreC# Program to find whether the Number is Divisible by 2
A number is divisible by 2 if the remainder is 0 when the number is divided by 2. This is checked using the modulo operator (%) in C#, which returns the remainder of a division operation. Numbers divisible by 2 are called even numbers, while numbers not divisible by 2 are called odd numbers. Divisibility by 2 Check Even Numbers num % 2 == 0 Examples: 2, 4, 6, 8, 10 Odd Numbers num % 2 ...
Read MoreCommon Language Runtime (CLR) in C#.NET
The Common Language Runtime (CLR) is the execution environment for .NET applications that manages code execution, memory allocation, and provides essential runtime services. It acts as an intermediary between your C# code and the operating system, converting intermediate language code into machine-specific instructions through just-in-time compilation. The CLR ensures that C# applications run safely and efficiently by providing automatic memory management, exception handling, type safety verification, and cross-language interoperability across all .NET languages. CLR Execution Process C# Code IL Code (Bytecode) ...
Read MoreWhat is a static constructor in C#?
A static constructor is a special constructor declared using the static modifier. It is the first block of code executed when a class is accessed for the first time. A static constructor executes only once during the entire lifetime of the application, regardless of how many instances of the class are created. Static constructors are primarily used to initialize static fields or perform one-time setup operations for a class. Syntax Following is the syntax for declaring a static constructor − static ClassName() { // initialization code } Key Rules of ...
Read MoreComparison of double and float primitive types in C#
In C#, float and double are both floating-point data types used to store decimal numbers, but they differ significantly in precision, memory usage, and range. Understanding these differences is crucial for choosing the right data type for your applications. Syntax Following is the syntax for declaring float and double variables − float floatVariable = 3.14f; double doubleVariable = 3.14159265359; Note the f suffix for float literals and optional d suffix for double literals − float price = 19.99f; double pi = 3.14159265359d; // 'd' is optional for double Key Differences ...
Read MoreChained Exceptions in C#
Chained exceptions in C# allow you to preserve the original exception information while throwing a new exception. This creates a chain of exceptions where each exception wraps the previous one, maintaining the complete error context throughout the call stack. When an exception occurs deep in your application, chained exceptions help maintain the full error history by wrapping the original exception inside a new one using the innerException parameter of the Exception constructor. Syntax Following is the syntax for creating a chained exception − try { // code that might throw exception } ...
Read More