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 170 of 196
How to create user defined exceptions in C#?
In C#, you can create user-defined exceptions by deriving from the Exception class or one of its derived classes. User-defined exceptions allow you to create specific error types that are meaningful to your application domain, making error handling more precise and informative. Syntax Following is the basic syntax for creating a user-defined exception class − public class CustomExceptionName : Exception { public CustomExceptionName() : base() { } public CustomExceptionName(string message) : base(message) { } public CustomExceptionName(string message, Exception innerException) : ...
Read MoreWhat are abstract classes in C#?
An abstract class in C# is a class that cannot be instantiated directly and contains one or more abstract methods that must be implemented by derived classes. Abstract classes provide a common base with shared functionality while requiring derived classes to implement specific methods with their own specialized behavior. Abstract classes are declared using the abstract keyword and serve as blueprints for related classes that share common characteristics but need different implementations for certain operations. Syntax Following is the syntax for declaring an abstract class with abstract methods − abstract class ClassName { ...
Read MoreWhat are abstract properties in C#?
An abstract property in C# is a property declared in an abstract class without implementation. The derived classes must provide the actual implementation of the property accessor (get, set, or both). Abstract properties enforce a contract that ensures all derived classes implement the required property. Abstract properties are useful when you want to define a common structure across related classes but need each class to calculate or store the property value differently. Syntax Following is the syntax for declaring an abstract property − public abstract class ClassName { public abstract DataType PropertyName ...
Read MoreWhat are the differences between public, protected and private access specifiers in C#?
Access specifiers in C# control the visibility and accessibility of class members. The three primary access specifiers − public, protected, and private − determine where class members can be accessed from, providing different levels of encapsulation and security. Syntax Following is the syntax for declaring members with different access specifiers − public class ClassName { public int publicField; // accessible everywhere protected int protectedField; // accessible in derived classes private int privateField; // accessible ...
Read MoreWhat are accessors of properties in C#?
Properties are an extension of fields and are accessed using the same syntax. They use accessors through which the values of the private fields can be read, written or manipulated. The accessor of a property contains the executable statements that help in getting (reading or computing) or setting (writing) the property. Properties provide a controlled way to access class fields while maintaining encapsulation. Syntax Following is the syntax for property accessors in C# − public datatype PropertyName { get { return fieldName; } ...
Read MoreWhat are Add, Remove methods in C# lists?
The List is a generic collection in C# that provides dynamic array functionality. The Add() and Remove() methods are fundamental operations for managing elements in a list, allowing you to add new items and remove existing ones efficiently. Syntax Following is the syntax for adding elements to a list − List listName = new List(); listName.Add(item); Following is the syntax for removing elements from a list − bool result = listName.Remove(item); listName.RemoveAt(index); Using Add() Method The Add() method appends an element to the end of the list. It increases ...
Read MoreWhat are all the possible C# array initialization syntaxes?
C# provides multiple ways to initialize arrays, giving you flexibility in how you declare and populate arrays with values. Each syntax has its specific use cases depending on whether you know the array size in advance or want to provide initial values immediately. Array Initialization Syntax Options Explicit Size with Values You can specify the array size explicitly along with initial values − int[] marks = new int[5] { 99, 98, 92, 97, 95 }; Implicit Size with Values The array size can be inferred from the number of elements provided − ...
Read MoreWhat are Base and Derived Classes in C#?
In C# object-oriented programming, inheritance allows you to create new classes based on existing ones. A base class (also called parent class) is the original class that provides common properties and methods. A derived class (also called child class) inherits from the base class and can access its members while adding its own specific functionality. The derived class automatically inherits all accessible members from the base class, including fields, properties, and methods, promoting code reusability and establishing an "is-a" relationship. Syntax Following is the syntax for creating a derived class in C# − class BaseClass ...
Read MoreWhat are Booleans types in C#?
The bool type in C# represents Boolean values and can store only two possible values: true and false. The bool keyword is an alias for the System.Boolean structure in the .NET framework. Boolean variables are commonly used in conditional statements, loops, and logical operations to control program flow based on true/false conditions. Syntax Following is the syntax for declaring Boolean variables − bool variableName = true; // or false bool variableName; // defaults to false Basic Boolean Declaration and Assignment Example ...
Read MoreHow to use StringBuilder in C#?
The StringBuilder class in C# is designed for efficient string manipulation when you need to perform multiple operations like appending, inserting, or replacing characters. Unlike regular strings, which are immutable and create new objects for every modification, StringBuilder maintains a mutable buffer that can be expanded without creating new objects in memory. Syntax Following is the syntax to initialize a StringBuilder − StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder(string value); StringBuilder sb = new StringBuilder(int capacity); StringBuilder sb = new StringBuilder(string value, int capacity); Key Advantages of StringBuilder Mutable ...
Read More