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
Articles by karthikeya Boyini
Page 11 of 143
Addition and Concatenation in C#
In C#, addition refers to mathematical operations with numeric types, while concatenation refers to joining strings together. The + operator can perform both operations depending on the operand types, and C# provides several methods for string concatenation. Syntax Following is the syntax for numeric addition − int result = number1 + number2; Following is the syntax for string concatenation using the + operator − string result = string1 + string2; Following is the syntax for string concatenation using String.Concat() method − string result = String.Concat(string1, string2); ...
Read MoreHow to swap two numbers without using a temp variable in C#
Swapping two numbers without using a temporary variable can be achieved using different techniques in C#. These methods eliminate the need for extra memory allocation and demonstrate clever mathematical and bitwise operations. Using Arithmetic Operations The most common approach uses addition and subtraction operations to swap values − val1 = val1 + val2; val2 = val1 - val2; val1 = val1 - val2; Example using System; class Program { static void Main(string[] args) { int val1, val2; ...
Read MoreHow to define a single-dimensional array in C Sharp?
An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type stored at contiguous memory locations. A single-dimensional array in C# is a linear collection of elements of the same data type. It allows you to store multiple values in a single variable and access them using an index. Syntax Following is the syntax for declaring a single-dimensional array − datatype[] arrayName = new datatype[size]; Following is the syntax for initializing an array with ...
Read MoreC# Program to find a key in a Hashtable
A Hashtable in C# is a collection of key-value pairs where each key is unique. To check if a specific key exists in a Hashtable, you can use the Contains() method or ContainsKey() method. Syntax Following is the syntax for creating a Hashtable and checking if a key exists − Hashtable hashtable = new Hashtable(); hashtable.Add(key, value); bool exists = hashtable.Contains(key); Alternatively, you can use ContainsKey() method − bool exists = hashtable.ContainsKey(key); Using Contains() Method The Contains() method returns true if the specified key exists in the Hashtable, otherwise ...
Read MoreAbstract vs Sealed Classes vs Class Members in C#
The abstract class includes abstract and non-abstract methods. You cannot instantiate an abstract class directly. The sealed class prevents inheritance and you cannot use it as a base class. Both abstract and sealed classes can contain various types of class members with different access modifiers and behaviors. Abstract Classes To declare an abstract class, you need to place the keyword abstract before the class definition. An abstract class can contain abstract methods that must be implemented by derived classes − Syntax public abstract class ClassName { public abstract void MethodName(); ...
Read MoreContainsKey() method in C#
The ContainsKey() method in C# is used to check whether a specific key exists in a Hashtable or other collection types like Dictionary. This method returns true if the key is found, otherwise it returns false. Syntax Following is the syntax for the ContainsKey() method − bool ContainsKey(object key) Parameters key − The key to locate in the collection. Return Value Returns true if the key exists in the collection; otherwise, false. ContainsKey() Method Logic Hashtable ...
Read MoreTimer in C#
The System.Timers.Timer class in C# generates events at specified intervals. It is useful for executing code periodically, such as updating displays, checking status, or performing background tasks. The timer can be configured to fire once or repeatedly, and provides precise control over timing intervals and event handling. Syntax Following is the syntax for creating and configuring a timer − Timer timer = new System.Timers.Timer(intervalInMilliseconds); timer.Elapsed += EventHandlerMethod; timer.Enabled = true; Following is the syntax for the event handler method − private static void EventHandlerMethod(Object source, ElapsedEventArgs e) { ...
Read MoreC# Program to get distinct element from a sequence
The Distinct() method in C# is used to remove duplicate elements from a sequence and return only unique values. This method is part of LINQ (Language Integrated Query) and works with any collection that implements IEnumerable. Syntax Following is the syntax for the Distinct() method − IEnumerable Distinct() IEnumerable Distinct(IEqualityComparer comparer) Parameters comparer (optional) − An IEqualityComparer to compare values for equality. If not provided, the default equality comparer is used. Return Value Returns an IEnumerable containing distinct elements from the source sequence. Using Distinct() with ...
Read MoreBackground and foreground thread in C#
A thread is defined as the execution path of a program. Each thread defines a unique flow of control. In C#, threads are classified as either foreground threads or background threads based on their behavior when the main application terminates. The key difference is that foreground threads keep the application alive, while background threads are automatically terminated when all foreground threads finish execution. Thread Types Comparison Foreground Thread Background Thread Keeps the application running until it completes Terminated automatically when all foreground threads end Default thread type (IsBackground ...
Read MoreHow to use sizeof() operator to find the size of a data type or a variable in C#
The sizeof operator in C# returns the size of a data type in bytes. It can only be used with value types and is particularly useful when working with unmanaged code or memory allocation scenarios. Syntax Following is the syntax for using the sizeof operator − sizeof(datatype) For example, to find the size of an int data type − sizeof(int); Key Rules The sizeof operator can only be used with value types, not reference types. It returns the size in bytes. For built-in ...
Read More