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 109 of 196
Overloaded method and ambiguity in C#
With method overloading, you can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. However, when using default parameters with overloaded methods, ambiguity can occur if the compiler cannot determine which method to call. Syntax Following is the syntax for method overloading − public void MethodName(int parameter1) { // Implementation 1 } public void MethodName(int parameter1, int parameter2) { // Implementation 2 ...
Read MorePrint with your own font using C#
To print with your own font in C#, you need to construct two essential objects: a FontFamily object and a Font object. The FontFamily object sets the typeface like Arial, Times New Roman, etc., whereas the Font object defines the size, style, and unit of measurement for the font. Syntax Following is the syntax for creating a FontFamily object − FontFamily fontFamily = new FontFamily("FontName"); Following is the syntax for creating a Font object − Font font = new Font(fontFamily, size, style, unit); Parameters The Font constructor accepts the ...
Read MorePrint Single and Multiple variable in C#
In C#, displaying variable values to the console is fundamental for debugging and output purposes. You can print single or multiple variables using various methods with Console.WriteLine(). Syntax Following is the syntax for printing a single variable − Console.WriteLine(variable); Console.WriteLine("Text: " + variable); Following is the syntax for printing multiple variables − Console.WriteLine("Values: {0} {1}", var1, var2); Console.WriteLine($"Values: {var1} {var2}"); Printing a Single Variable You can print a single variable by passing it directly to Console.WriteLine() or by concatenating it with text using the + operator − ...
Read MorePriority Queues with C#
A priority queue is a data structure that stores elements with associated priority values. It is an extension of a regular queue where elements are served based on their priority rather than their insertion order. When you remove an item from a priority queue, the element with the highest priority is removed first. If two elements have the same priority, they are typically served in the order they were inserted. Syntax Following is the basic syntax for creating a priority queue class − public class MyPriorityQueue where T : IComparable { private ...
Read MorePrivate Constructors and Singleton Classes in C#
A private constructor is a constructor that cannot be called from outside the class. It is primarily used in classes containing only static members or to implement the Singleton design pattern, which ensures that only one instance of a class can exist throughout the application's lifetime. Syntax Following is the syntax for declaring a private constructor − class ClassName { private ClassName() { // private constructor body } } Private Constructor for Static-Only Classes When a class contains only static members, ...
Read MoreOptimization Tips for C# Code
C# code optimization involves writing efficient code that executes faster and uses memory more effectively. Here are essential optimization tips that can significantly improve your application's performance. Prefer Generic Collections Over Non-Generic Use List instead of ArrayList whenever possible. Generic collections provide type safety and better performance by avoiding boxing and unboxing operations − using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; class Program { public static void Main() { // Inefficient - ArrayList with boxing ArrayList arrayList = new ...
Read MorePrivate Variables in C#
The private access specifier in C# allows a class to hide its member variables and member functions from other functions and objects. Only methods of the same class can access its private members. Even an instance of a class cannot access its private members directly from outside the class. Syntax Following is the syntax for declaring a private variable − private dataType variableName; For example − private double length; private int count; private string name; Key Rules of Private Variables Private members can only be accessed within ...
Read MoreOptional property in a C# class
An optional property in a C# class is a property that can have a null value without causing runtime errors. Properties whose CLR types cannot hold null values (like value types) cannot be configured as optional unless they are made nullable using the ? operator. Optional properties are commonly implemented using nullable reference types, default values, or custom attributes to indicate that the property is not required to have a value. Syntax Following is the syntax for declaring nullable properties − public string? PropertyName { get; set; } // Nullable reference type ...
Read MoreMutation Test tools in C#
Mutation testing is a software testing technique that evaluates the quality of your test suite by introducing small code changes (mutations) and checking if your tests can detect these changes. In C#, several tools are available to perform mutation testing effectively. What is Mutation Testing? Mutation testing works by creating mutants − modified versions of your source code with small, intentional bugs. A good test suite should detect these mutations and fail when run against the mutated code. The mutation score represents the percentage of mutants killed (detected) by your tests. Mutation Testing ...
Read MoreNaming Conventions in C#
Naming conventions in C# help maintain code readability and consistency across projects. Following standardized naming patterns makes your code more professional and easier to understand for other developers. Class Naming Conventions A class definition starts with the class keyword followed by the class name, enclosed by curly braces. The following conventions apply to class names. Pascal Casing Class names should use PascalCasing, where the first letter of each word is capitalized − public class EmployeeDetails { } public class BankAccount { } public class CustomerOrderHistory { } Noun or Noun Phrases ...
Read More