Deconstructors in C# 7.0 are methods that allow you to extract multiple values from an object into separate variables in a single assignment. They enable tuple-like deconstruction of custom types, making it easier to work with objects that contain multiple related values. A deconstructor is defined using the Deconstruct method name with out parameters. When you use tuple syntax on the left side of an assignment, C# automatically calls the appropriate Deconstruct method. Syntax Following is the syntax for defining a deconstructor method − public void Deconstruct(out Type1 param1, out Type2 param2, out Type3 param3) ... Read More
The Queue.Clone() method in C# is used to create a shallow copy of the Queue. This method returns a new Queue object that contains references to the same elements as the original Queue, but the Queue structure itself is independent. Syntax Following is the syntax for the Queue.Clone() method − public virtual object Clone(); Return Value The method returns an object that represents a shallow copy of the Queue. You need to cast it back to Queue type to use it as a Queue. Understanding Shallow Copy A shallow copy means ... Read More
The OrderBy and GroupBy operators are essential LINQ methods in C# that allow you to sort and organize data collections. OrderBy sorts sequences in ascending order, while GroupBy organizes flat sequences into groups based on a specified key. These operators are particularly useful when working with collections of objects that need to be organized or sorted for better data presentation and analysis. Syntax Following is the syntax for OrderBy operator − var result = collection.OrderBy(x => x.PropertyName); var resultDesc = collection.OrderByDescending(x => x.PropertyName); Following is the syntax for GroupBy operator − ... Read More
Local functions are private methods defined inside another member (method, constructor, or property). They are only accessible within their containing member and provide a clean way to break down complex logic into smaller, reusable pieces without exposing helper methods to the entire class. Local functions were introduced in C# 7.0 and offer better performance than lambda expressions for recursive scenarios and provide compile-time checking for definite assignment. Syntax Following is the syntax for declaring a local function − returnType LocalFunctionName(parameters) { // function body } Local functions can be ... Read More
C# interface members can be implemented in two ways: implicitly and explicitly. With implicit implementation, the implementing class doesn't include the interface name before the member name, making the compiler infer the interface connection automatically. In implicit implementation, the members are exposed as public and are accessible when the object is cast as either the concrete type or the interface type. This approach is simpler and more commonly used when there are no naming conflicts between interface methods. Syntax Following is the syntax for implicit interface implementation − interface IInterfaceName { void ... Read More
ViewBag uses the dynamic feature that was introduced in C# 4.0. It allows an object to have properties dynamically added to it. Internally, it is a dynamic type property of the ControllerBase class which is the base class of the Controller class. ViewBag only transfers data from controller to view, not vice-versa. ViewBag values will be null if redirection occurs. ViewBag is able to set and get value dynamically and able to add any number of additional fields without converting it to strongly typed. Syntax Following is the syntax for storing data in ViewBag − ... Read More
C# 7.0 introduced two important enhancements to numeric literals that improve code readability and provide new ways to represent numbers: binary literals and digit separators. These features make it easier to work with binary values and large numbers in your code. Binary Literals Before C# 7.0, you could only assign decimal and hexadecimal values to variables. Binary literals allow you to directly assign binary values using the 0b or 0B prefix, making it easier to work with bit flags, masks, and other binary operations. Syntax var binaryNumber = 0b10101010; // Binary literal var binaryNumber2 ... Read More
The NonAction attribute in ASP.NET MVC is used to prevent public methods in a controller from being treated as action methods. By default, all public methods in an MVC controller are accessible via URL routing. The NonAction attribute allows you to keep methods public (for internal use within the controller or class hierarchy) while blocking direct HTTP access. Syntax Following is the syntax for applying the NonAction attribute − [NonAction] public ReturnType MethodName() { // method implementation } Why Use NonAction Attribute In ASP.NET MVC, the framework automatically treats ... Read More
The Queue.Contains() method in C# is used to determine whether a specific element exists in the Queue. It returns true if the element is found, otherwise false. This method performs a linear search through the queue elements. Syntax Following is the syntax for the Queue.Contains() method − public bool Contains(T item); Parameters item − The object to locate in the Queue. The value can be null for reference types. Return Value Returns true if the item is found in the Queue; otherwise, false. ... Read More
C# 7.0 introduced ref locals and ref returns, which allow methods to return references to variables instead of copies of their values. This enables direct modification of the original data through the returned reference. A ref return allows a method to return a reference to a variable, and the caller can create a ref local variable that directly references the returned memory location. Syntax Following is the syntax for declaring a ref return method − public static ref ReturnType MethodName(parameters) { return ref variable; } Following is the syntax ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance