How to implement interface in anonymous class in C#?

王林
Release: 2023-08-31 20:25:02
forward
768 people have browsed it

How to implement interface in anonymous class in C#?

No, anonymous types cannot implement interfaces. We need to create your own type.

Anonymous types provide a convenient way to encapsulate a set of read-only types Properties are converted to a single object without first explicitly defining the type.

Type names are generated by the compiler and are not available in the source code grade. The type of each property is inferred by the compiler.

You can create anonymous types by using the new operator with an object Initialization program.

Example

class Program{ public static void Main(){ var v = new { Amount = 108, Message = "Test" }; Console.WriteLine(v.Amount + v.Message); Console.ReadLine(); } }
Copy after login

Output

108Test
Copy after login

The above is the detailed content of How to implement interface in anonymous class in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!