Csharp Articles

Page 97 of 196

How to calculate Power of a number using recursion in C#?

George John
George John
Updated on 17-Mar-2026 1K+ Views

To calculate power of a number using recursion in C#, we use the mathematical principle that n^p = n × n^(p-1). The recursive function calls itself with a reduced power until it reaches the base case. Syntax Following is the syntax for a recursive power function − static long Power(int number, int power) { if (power == 0) { return 1; // base case } return number * Power(number, power - 1); // recursive ...

Read More

What are pointers in C#?

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

A pointer is a variable that stores the memory address of another variable. Unlike reference types, pointers provide direct access to memory locations, making them powerful but potentially unsafe. In C#, pointers can only be used within unsafe code blocks or methods. This restriction exists because pointers bypass .NET's garbage collection and type safety mechanisms. Syntax Following is the syntax for declaring a pointer − type *pointerName; Following is the syntax for getting the address of a variable − type *pointer = &variable; Following is the syntax for dereferencing ...

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

What is method overloading in C#?

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 2K+ Views

Method overloading in C# allows you to define multiple methods with the same name but different parameters. This enables you to create methods that perform similar operations but accept different types or numbers of arguments. Method overloading can be achieved by changing the number of parameters, the data types of parameters, or the order of parameters. The compiler determines which method to call based on the arguments passed at runtime. Syntax Following is the syntax for method overloading − public returnType MethodName(type1 param1) { } public returnType MethodName(type1 param1, type2 param2) { } public returnType ...

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

What is compile time polymorphism in C#?

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

Compile-time polymorphism in C# is a type of polymorphism where the method to be called is determined during compilation rather than at runtime. It is also known as static polymorphism or early binding, because the method binding occurs at compile time. C# provides two main techniques to implement compile-time polymorphism − Method Overloading and Operator Overloading. The compiler resolves which method to call based on the method signature (method name, number of parameters, and parameter types). Compile-time Polymorphism Method Resolution at Compile Time Compiler determines which method ...

Read More

What is composition in C#?

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

Composition in C# is a design principle where one class contains an instance of another class as a private member, creating a "has-a" relationship. In composition, the contained object (child) cannot exist independently of the container object (parent). When the parent object is destroyed, the child object is also destroyed, establishing a strong ownership relationship. Composition represents a part-of relationship, which is a special type of association that implies strong coupling between objects. Syntax Following is the basic syntax for implementing composition in C# − public class ContainedClass { // properties and ...

Read More

What is a parameterized constructor in C# programs?

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 3K+ Views

A parameterized constructor in C# is a constructor that accepts parameters to initialize an object with specific values at the time of creation. This allows you to set initial values for the object's fields or properties during instantiation, making object creation more flexible and efficient. Syntax Following is the syntax for declaring a parameterized constructor − public ClassName(dataType parameter1, dataType parameter2) { // initialization code this.field1 = parameter1; this.field2 = parameter2; } To create an object using a parameterized constructor − ClassName objectName ...

Read More

What is late binding in C#?

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

Late binding in C# refers to the process where the decision of which method to call is made at runtime rather than compile time. This is also known as dynamic polymorphism, where the actual method implementation is determined by the object type at runtime, not by the reference type. Late binding is implemented using virtual methods in base classes and override methods in derived classes. When a virtual method is called through a base class reference, the runtime determines which derived class method to execute based on the actual object type. How Late Binding Works In late ...

Read More

What does the interface IStructuralEquatable do in C#?

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

The IStructuralEquatable interface in C# defines methods to support the comparison of objects for structural equality. This means two objects are considered equal if they have the same structure and equal values, rather than being the same reference. This interface is particularly useful for collections like arrays, tuples, and other composite objects where you want to compare the actual content rather than object references. Syntax The interface defines two methods − public interface IStructuralEquatable { bool Equals(object other, IEqualityComparer comparer); int GetHashCode(IEqualityComparer comparer); } Methods ...

Read More
Showing 961–970 of 1,951 articles
« Prev 1 95 96 97 98 99 196 Next »
Advertisements