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 131 of 196
How to access array elements using a pointer in C#?
In C#, accessing array elements using pointers requires understanding the key differences between arrays and pointers. An array name and a pointer to the same data type are not the same variable type. For example, int *p and int[] p represent different types. You can increment a pointer variable because it's not fixed in memory, but an array address is fixed in memory and cannot be incremented directly. To access array elements using pointers in C#, you must use unsafe code and the fixed statement to pin the array in memory temporarily. Syntax Following is the syntax ...
Read MoreWhat is the difference between virtual and abstract functions in C#?
In C#, both virtual and abstract methods enable polymorphism but serve different purposes. Virtual methods provide a default implementation that can be overridden, while abstract methods have no implementation and must be overridden by derived classes. Understanding the difference between these two concepts is crucial for implementing proper inheritance hierarchies and achieving runtime polymorphism in your applications. Syntax Following is the syntax for declaring a virtual method − public virtual returnType MethodName() { // default implementation } Following is the syntax for declaring an abstract method − ...
Read MoreC# program to convert an array to an ordinary list with the same items
Converting an array to a list is a common operation in C#. There are several ways to achieve this conversion, from manual iteration to using built-in methods that make the process more efficient and concise. Using Manual Loop The most straightforward approach is to create an empty list and add each array element using a loop − using System; using System.Collections.Generic; public class Program { public static void Main() { int[] arr = { 23, 66, 96, 110 }; var ...
Read MoreFile Permissions in C#
File permissions in C# are managed using the FileIOPermission class from the System.Security.Permissions namespace. This class controls the ability to access files and folders by defining what operations are allowed on specific file system resources. The FileIOPermission class is part of .NET's Code Access Security (CAS) system and helps ensure that applications only perform file operations they are explicitly granted permission to execute. Syntax Following is the syntax for creating a FileIOPermission instance − FileIOPermission permission = new FileIOPermission(PermissionState.None); permission.AllLocalFiles = FileIOPermissionAccess.Read; Following is the syntax for adding specific path permissions − ...
Read MorePair Class in C#
The KeyValuePair structure in C# allows you to store a pair of related values as a single unit. It is commonly used in collections like Dictionary and can be used in lists to store paired data such as name-value combinations or key-identifier pairs. The KeyValuePair is a generic structure that provides a way to store two related pieces of data together, where TKey represents the key type and TValue represents the value type. Syntax Following is the syntax for creating a KeyValuePair − KeyValuePair pair = new KeyValuePair(key, value); Following is the syntax ...
Read MoreScope of Variables in C#
The scope of a variable in C# determines the region of code where a variable can be accessed and used. Understanding variable scope is crucial for writing efficient and error-free programs. C# has several levels of variable scope, each with different accessibility rules and lifetimes. Types of Variable Scope Method Level (Local Variables) Variables declared inside a method are local variables. They are only accessible within that specific method and are destroyed when the method execution completes − using System; class Program { public void TestMethod() { ...
Read MoreHow to clear screen using C#?
The Console.Clear() method in C# is used to clear the console screen and its buffer. When this method is called, the cursor automatically moves to the top-left corner of the console window, effectively giving you a clean slate to work with. Syntax Following is the syntax for the Console.Clear() method − Console.Clear(); How It Works The Console.Clear() method performs the following actions − Clears all text and content from the console window Resets the cursor position to (0, 0) — the top-left corner Preserves the current ...
Read MoreAppend to StringBuilder in C#
The Append() method in C# is used to add content to a StringBuilder object. Unlike strings, which are immutable, StringBuilder allows efficient concatenation of text without creating new string objects in memory. StringBuilder is particularly useful when you need to perform multiple string concatenations, as it provides better performance than using the + operator repeatedly. Syntax Following is the syntax for creating a StringBuilder and using the Append() method − StringBuilder sb = new StringBuilder(); sb.Append(value); The Append() method can accept various data types including string, char, int, double, and others. It returns ...
Read MoreReplace a string using StringBuilder
The StringBuilder class in C# provides an efficient way to modify strings without creating new string objects. The Replace() method allows you to replace all occurrences of a specified string or character with another string or character directly within the StringBuilder object. Syntax Following is the syntax for the Replace() method in StringBuilder − public StringBuilder Replace(string oldValue, string newValue) public StringBuilder Replace(char oldChar, char newChar) public StringBuilder Replace(string oldValue, string newValue, int startIndex, int count) Parameters oldValue/oldChar − The string or character to be replaced. newValue/newChar − The string or character ...
Read MoreCompare the content of two StringBuilders
The Equals method in C# is used to compare the content of two StringBuilder objects. This method performs a character-by-character comparison of the text content stored in both StringBuilder instances. Syntax Following is the syntax for comparing two StringBuilder objects − bool result = stringBuilder1.Equals(stringBuilder2); Parameters The Equals method takes one parameter − sb − The StringBuilder object to compare with the current instance. Return Value The method returns a bool value − true if both StringBuilder objects have the same content. ...
Read More