Home  >  Article  >  Backend Development  >  How to implement interface in anonymous class in C#?

How to implement interface in anonymous class in C#?

王林
王林forward
2023-08-31 20:25:02933browse

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();
   }
}

Output

108Test

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete