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 on Trending Technologies
Technical articles with clear explanations and examples
What are user defined data types in C#?
User-defined data types in C# allow developers to create custom types beyond the built-in primitive types. The main user-defined data types in C# are structures, enumerations, classes, interfaces, and delegates. This article focuses on structures and enumerations as fundamental user-defined types. Structure In C#, a structure is a value type data type that helps you group related data of various types into a single unit. The struct keyword is used for creating a structure. Syntax public struct StructName { // fields, properties, methods, constructors public dataType field1; ...
Read MoreHow to iterate over a C# tuple?
A tuple in C# is a data structure that holds multiple values of different types. Since tuples have a fixed structure, you cannot iterate over them using traditional loops like arrays or collections. Instead, you access tuple elements individually using their Item properties or through deconstruction. Syntax To create a tuple and access its elements − // Creating a tuple Tuple tuple = new Tuple(100, "Tom"); // Accessing individual elements tuple.Item1 // first element tuple.Item2 // second element For modern C# (7.0+), you can use tuple literals − var ...
Read MoreFunc generic type in C#
The Func generic type in C# is a built-in delegate that represents a method which takes zero or more input parameters and returns a value. It provides a convenient way to store anonymous methods, lambda expressions, and regular methods that return a value. Syntax Following is the syntax for declaring a Func delegate − Func funcName; // No parameters, returns TResult Func funcName; ...
Read MoreC# Math.DivRem Method
The Math.DivRem method in C# performs integer division and returns both the quotient and remainder in a single operation. This is more efficient than performing separate division and modulo operations when you need both results. Syntax Following is the syntax for the Math.DivRem method − public static int Math.DivRem(int a, int b, out int result); public static long Math.DivRem(long a, long b, out long result); Parameters a − The dividend (number to be divided) b − The divisor (number to divide by) result − An out parameter that receives the remainder ...
Read MoreType.GetTypeHandle() Method in C#
The Type.GetTypeHandle() method in C# is used to get the runtime type handle for a specified object. A RuntimeTypeHandle provides a lightweight way to reference a type without holding a direct reference to the Type object itself, which is useful in performance-critical scenarios and interop operations. Syntax Following is the syntax for the Type.GetTypeHandle() method − public static RuntimeTypeHandle GetTypeHandle(object o) Parameters o − The object for which to get the type handle. Return Value Returns a RuntimeTypeHandle structure that represents the type of the specified object. Using ...
Read MoreC# Program to Kill a Thread
In C#, a thread can be gracefully stopped using a flag-based approach rather than forcibly terminating it. This method allows the thread to complete its current work and exit cleanly by checking a boolean flag in its loop condition. The recommended approach involves setting a boolean flag that the thread periodically checks. When you want to stop the thread, you set the flag to true, causing the thread's loop to exit naturally. Syntax Following is the syntax for creating and controlling a thread with a stop flag − Thread thread = new Thread(methodName); thread.Start(); ...
Read MoreHow do I determine the size of my array in C#
In C#, determining the size of an array is accomplished using the Length property. This property returns the total number of elements in the array as an int value. Syntax Following is the syntax for getting array length − arrayName.Length Parameters The Length property does not take any parameters and returns an integer representing the number of elements. Return Value Returns an int value indicating the total number of elements in the array. Using Length Property for Single-Dimensional Arrays For single-dimensional arrays, the Length property provides a direct count ...
Read MoreType.GetFields() Method in C#
The Type.GetFields() method in C# is used to retrieve the fields of the current Type using reflection. This method returns an array of FieldInfo objects representing the fields that match the specified binding criteria. Fields are data members of a class that store values. The GetFields() method allows you to examine both public and non-public fields at runtime, making it useful for inspection, debugging, and dynamic programming scenarios. Syntax Following is the syntax for the parameterless overload − public System.Reflection.FieldInfo[] GetFields(); Following is the syntax for the overload with binding flags − ...
Read MoreC# Program to pass Parameter to a Thread
To work with threads in C#, you need to add the System.Threading namespace. Passing parameters to threads allows you to send data from the main thread to worker threads, enabling more flexible and dynamic thread operations. Syntax Following is the syntax for creating a thread and passing a parameter − Thread thread = new Thread(ThreadMethod); thread.Start(parameter); The thread method must accept an object parameter − static void ThreadMethod(object parameter) { // cast parameter to appropriate type } Using Thread.Start() with a Parameter The Thread.Start(object) method accepts ...
Read MoreRepresent Int32 as a String in C#
The Int32 type in C# represents a 32-bit signed integer. To convert an Int32 value to its string representation, you can use the ToString() method. This method converts the numeric value into a readable string format. Syntax Following is the syntax for converting an Int32 to string using ToString() − int number = value; string result = number.ToString(); You can also use format specifiers with ToString() − string result = number.ToString("format"); Using ToString() Method Basic Conversion Example The simplest way to convert an Int32 to string is using ...
Read More