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
Csharp Articles
Page 97 of 196
How to calculate Power of a number using recursion in C#?
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 MoreWhat are pointers in C#?
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 MoreHow to pass pointers as parameters to methods in C#?
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 MoreWhat is method overloading in C#?
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 MoreWhat are the main parts of a C# program?
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 MoreWhat is compile time polymorphism in C#?
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 MoreWhat is composition in C#?
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 MoreWhat is a parameterized constructor in C# programs?
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 MoreWhat is late binding in C#?
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 MoreWhat does the interface IStructuralEquatable do in C#?
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