Csharp Articles

Page 61 of 196

How to implement interface in anonymous class in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 2K+ Views

No, anonymous types cannot implement an interface. We need to create your own type. Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler. Syntax Following is the syntax for creating anonymous types − var anonymousObject = new { Property1 = value1, Property2 = value2 }; Why Anonymous Types Cannot Implement ...

Read More

What is difference between using if/else and switch-case in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 555 Views

The switch statement and if-else statements are both selection statements in C# used for decision-making, but they serve different purposes and have distinct performance characteristics. The switch statement chooses a single section to execute from a list of candidates based on pattern matching, while if-else provides conditional branching based on boolean expressions. Syntax Following is the syntax for a switch statement − switch (expression) { case value1: // code block break; case ...

Read More

What are the improvements in Out Parameter in C# 7.0?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 256 Views

C# 7.0 introduced significant improvements to out parameters that make code more concise and readable. The major enhancement is the ability to declare out variables inline as arguments to the method where they're used, eliminating the need for separate variable declarations. Syntax Prior to C# 7.0, out variables had to be declared before use − int result; if (int.TryParse("123", out result)) { // use result } C# 7.0 allows inline declaration − if (int.TryParse("123", out int result)) { // use result immediately } Key ...

Read More

What are Deconstructors in C# 7.0?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 252 Views

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

What are Local functions in C# 7.0?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 247 Views

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

What are binary literals and digit separators in C# 7.0?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 483 Views

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

What are Ref locals and Ref returns in C# 7.0?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 720 Views

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

What are Async Streams in C# 8.0?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 252 Views

C# 8.0 introduces async streams, which enable asynchronous iteration over data that is generated or retrieved asynchronously. Unlike regular streams that return all data at once, async streams produce elements one at a time as they become available. Async streams use the IAsyncEnumerable interface and allow methods to use yield return with the async modifier. You can consume async streams using await foreach loops, which asynchronously wait for each element. Syntax Following is the syntax for declaring an async stream method − static async IAsyncEnumerable MethodName() { await SomeAsyncOperation(); ...

Read More

What are the advantages of using C# ASP.NET WebAPI?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 2K+ Views

ASP.NET Web API is a framework for building HTTP-based services that can be consumed by a broad range of clients including browsers, mobile applications, and desktop applications. It provides numerous advantages over traditional web services and other communication technologies. Key Advantages of ASP.NET Web API HTTP-Based Architecture Web API works seamlessly with HTTP protocols using standard HTTP verbs like GET, POST, PUT, and DELETE for CRUD operations. This makes it intuitive and follows REST principles − [HttpGet] public IActionResult GetUsers() { } [HttpPost] public IActionResult CreateUser([FromBody] User user) { } [HttpPut("{id}")] ...

Read More

How to configure C# ASP.NET WebAPI in web.configure file?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 1K+ Views

ASP.NET Web API uses code-based configuration rather than XML-based configuration in web.config. While you cannot configure Web API routing and behavior directly in web.config, you can configure it programmatically in the WebApiConfig.cs file or during application startup. Configuration Location Web API configuration is typically done in the Register method of the WebApiConfig class, which is called during application startup − public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API configuration goes here } } ...

Read More
Showing 601–610 of 1,951 articles
« Prev 1 59 60 61 62 63 196 Next »
Advertisements