Csharp Articles

Page 55 of 196

How can we return multiple values from a function in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 3K+ Views

In C#, there are several approaches to return multiple values from a function. This is useful when you need to return more than one result from a single method call. The main approaches are − Reference parameters using ref keyword Output parameters using out keyword Returning an Array of values Returning a Tuple object Using Reference Parameters The ref keyword passes arguments by reference, allowing the method to modify the original variable. The variable must be initialized before passing it to the method. Syntax ...

Read More

How do you give a C# Auto-Property a default value?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 2K+ Views

In C#, auto-properties provide a shorthand way to declare properties without explicitly writing getter and setter methods. Setting default values for auto-properties can be done in two main ways: using constructor initialization (available in all C# versions) or using property initializers (introduced in C# 6.0). Syntax Constructor initialization syntax (C# 5.0 and earlier) − public class ClassName { public PropertyType PropertyName { get; set; } public ClassName() { PropertyName = defaultValue; ...

Read More

How to call a static constructor or when static constructor is called in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 913 Views

A static constructor in C# is called automatically by the Common Language Runtime (CLR) before the first instance of a class is created or any static members are referenced. It is used to initialize static data or perform actions that need to be executed only once during the application's lifetime. Unlike instance constructors, static constructors cannot be called directly and have no control over when they execute − the CLR handles their invocation automatically. Syntax Following is the syntax for declaring a static constructor − static ClassName() { // initialization code } ...

Read More

How can we call one constructor from another in the same class in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 2K+ Views

In C#, you can call one constructor from another constructor within the same class using the this keyword. This technique is called constructor chaining. To call a constructor from a parent class, use the base keyword. Syntax Following is the syntax for calling another constructor in the same class using this − public ClassName(parameters) : this(arguments) { // additional initialization code } Following is the syntax for calling a parent class constructor using base − public DerivedClass(parameters) : base(arguments) { // derived class initialization code } ...

Read More

How to prove that only one instance of the object is created for static class?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 261 Views

In C#, a static class ensures that only one instance exists throughout the application's lifetime. To prove this concept, we can demonstrate how static variables maintain their state across multiple accesses, and how the static constructor is called only once. A static class cannot be instantiated using the new keyword. Instead, all members belong to the type itself rather than to any specific instance. This behavior proves that only one "instance" of the static class exists in memory. Syntax Following is the syntax for declaring a static class with static members − static class ClassName ...

Read More

How many destructors can we have in one class in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 540 Views

In C#, a destructor is a special method used to clean up resources when an object is destroyed. The answer to how many destructors a class can have is simple: only one destructor per class. Destructors are also called finalizers because they are automatically converted to Finalize() method calls by the compiler. Syntax Following is the syntax for declaring a destructor − class ClassName { ~ClassName() { // cleanup code } } Properties of Destructors Destructors do not ...

Read More

How to explicitly call base class constructor from child class in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 1K+ Views

The base keyword in C# is used to explicitly call a constructor from the parent class when creating an instance of a derived class. This is essential when the base class doesn't have a parameterless constructor or when you need to pass specific values to initialize the base class properly. By default, C# automatically calls the parameterless constructor of the base class. However, when the base class only has parameterized constructors, you must explicitly specify which constructor to call using the base keyword. Syntax Following is the syntax for calling a base class constructor from a derived ...

Read More

Which one is better Build, Rebuild, or Clean in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 325 Views

When working with C# projects in Visual Studio, you have three main build options: Build, Rebuild, and Clean. Each serves a different purpose and understanding when to use each one can significantly improve your development workflow and troubleshoot compilation issues. Build Solution The Build option performs an incremental build, which means it only compiles code files that have changed since the last build. This is the most efficient option for regular development work. Key characteristics of Build − Only compiles modified files and their dependencies Fastest build option for development Preserves existing compiled files that ...

Read More

What is explicit implementation and when to use in the interface in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 421 Views

Explicit interface implementation in C# allows a class to implement interface members in a way that they can only be accessed through the interface reference, not through the class instance directly. This is particularly useful when a class implements multiple interfaces that have members with the same signature. Syntax Following is the syntax for explicit interface implementation − interface IInterface1 { void Method(); } class MyClass : IInterface1 { void IInterface1.Method() { // explicit implementation } } Note ...

Read More

What is the difference between Finalize and Dispose in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 3K+ Views

The Finalize and Dispose methods in C# are both used for cleaning up resources, but they work differently and serve distinct purposes in memory management. Understanding their differences is crucial for proper resource management in .NET applications. Finalize Method The Finalize() method is called automatically by the Garbage Collector before an object eligible for collection is reclaimed. The Garbage Collector takes responsibility for deallocating memory for unreferenced objects. This method is called at some point after there are no longer valid references to that object in memory. The framework does not guarantee when this will happen. While ...

Read More
Showing 541–550 of 1,951 articles
« Prev 1 53 54 55 56 57 196 Next »
Advertisements