
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to implement interface in anonymous class in C#?
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.
You create anonymous types by using the new operator together with an object initializer.
Example
class Program{ public static void Main(){ var v = new { Amount = 108, Message = "Test" }; Console.WriteLine(v.Amount + v.Message); Console.ReadLine(); } }
Output
108Test
- Related Articles
- How to implement an interface using an anonymous inner class in Java?
- How to implement lambda expression without creating an anonymous class in Java?
- How to create an instance of an anonymous interface in Kotlin?
- How to implement dependency injection using Interface-based injection in C#?
- How to implement Flow.Publisher interface in Java 9?
- What is Interface segregation principle and how to implement it in C#?
- How to use interface with class in TypeScript?
- How to create a thread by using anonymous class in Java?
- How to implement a Set interface in JShell in Java 9?
- How to implement ObjLongConsumer interface using lambda expression in Java?
- How to implement class constants in TypesScript?
- Golang program to implement recursive anonymous function
- Using class to implement Vector Quantities in C++
- Why an interface cannot implement another interface in Java?
- Must we implement all the methods in a class that implements an interface in Java?

Advertisements