匿名内部类_匿名

匿名内部类

Anonymous Types in C#

C#中的匿名类型

by Ivo Stoykov

通过伊沃·斯托伊科夫(Ivo Stoykov)

Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read only data into an anonymous type without first defining object explicitly. (Anonymous simply means "without a name".)

当我们不需要遵循通常的工作流程时,匿名类型非常有用-创建某种类型的对象,分配一些只读值,然后对它们进行一些处理。 相反,我们可以将此只读数据封装为匿名类型,而无需先明确定义对象。 (匿名仅表示“没有名称”。)

Anonymous types are class types that consist of one or more public read-only properties.

匿名类型是由一个或多个公共只读属性组成的类类型。

In C# 3.0 compiler generates anonymous types through object initializer, who cares to use appropriate types assigning given values to one or more fields or properties of an object. This is why anonymous types are not available in source code level.

在C#3.0中,编译器通过

Objects declared as anonymous types use the var keyword in place of the type name. Dislike JavaScript the var keyword here always generates a strongly typed variable reference.

声明为匿名类型的对象使用var关键字代替类型名称。 与JavaScript不同,这里的var关键字总是生成强类型变量引用。

Sample:

样品:

var v = new { Amount = 108, Message = "Hello" };

Note that there is no name for the class. Therefore, in declaration, we cannot specify type in front of the variable. Instead the var keyword is used.

请注意,该类没有名称。 因此,在声明中,我们不能在变量前面指定类型。 而是使用var关键字。

var tells the compiler that we leave to it to infer the type of the variable for us. The var keyword can be used to reference any type in C#. So following declaration

var告诉编译器,我们只能依靠它来为我们推断变量的类型。 var关键字可用于引用C#中的任何类型。 所以下面的声明

var name = "Scott";
String name = "Scott";

Because the var keyword produces a strongly-typed variable declaration, the compiler needs to be able to infer the type to declare based on its usage.  This means that you need to always do an initial value assignment when declaring one. The compiler will produce a compiler error if you don't:

因为var关键字生成强类型变量声明,所以编译器需要能够根据其用法推断要声明的类型。 这意味着在声明一个值时始终需要进行初始值分配。 如果不这样做,编译器将产生一个编译器错误:

var name; // will generate error
var name = "Scott"; // will compile clean

Note that in the sample above

请注意,在上面的示例中

var v = new { Amount = 108, Message = "Hello" };

new keyword. It is used without a type name. What follows the new keyword is the object initializer mentioned previously. It is its duty to create a class for us with declared properties.

关键字。 它不带类型名使用。 new关键字之后是前面提到的

So for our v variable it will create

因此,对于我们的v变量,它将创建

class __NONAME__
{
  public int Amount { get; set; }
  public string Message  { get; set; }
}

Note  that object initializer works out the type from the expression used to initialize the variable when it is first declared.

请注意,

Sample:

样品:

// Program.cs
using System;
using System.Query;
using System.Data.DLinq;
namespace AnonTypes
{
   class Program
   {
      static void Main(string[] args)
      {
         var p1 = new {Name = "A", Price = 3}; /* creates new anonymous type with String value A for property Name and int value 3 for property Price */
         Console.WriteLine("Name = {0}\nPrice = {1}",
                           p1.Name, p1.Price);
         Console.ReadLine();
      }
   }
}

Limitation

局限性

Anonymous Type (type = class in this case) are greatly limited compared to standard classes. They can only inherit from object and their only memebers are private fields.

与标准类相比,匿名类型(在这种情况下,类型=类)受到极大限制。 它们只能从对象继承,并且它们的唯一成员是私有字段。

翻译自: https://www.experts-exchange.com/articles/2113/Be-anonymous.html

匿名内部类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值