简体   繁体   English

C#-Event / EventHandler语法

[英]C# - Event/EventHandler syntax

In my ASP.NET .NET 4 book, I'm seeing this syntax: 在我的ASP.NET .NET 4书中,我看到了以下语法:

public event EventHandler<AddressEventArgs> SaveButtonClick;

I'm not familiar with this syntax, especially the '< >' next to the event handler. 我不熟悉此语法,尤其是事件处理程序旁边的“ <>”。 Could someone please explain the above code? 有人可以解释上面的代码吗?

Full code example from the book is below. 这本书的完整代码示例如下。

//Declare custom EventArgs to be used
public class AddressEventArgs : EventArgs {
    public AddressEventArgs(string addressLine1, string addressLine2,
    string city, string state, string postalCode) {
        this.AddressLine1 = addressLine1;
        this.AddressLine2 = addressLine2;
        this.City = city;
        this.State = state;
        this.PostalCode = postalCode;
    }
    public string AddressLine1 { get; private set; }
    public string AddressLine2 { get; private set; }
    public string City { get; private set; }
    public string State { get; private set; }
    public string PostalCode { get; private set; }
}

//Code in a user control raising the event on a button click
public event EventHandler<AddressEventArgs> SaveButtonClick;
protected void ButtonSave_Click(object sender, EventArgs e) {
    if (SaveButtonClick != null) {
        SaveButtonClick(this, new AddressEventArgs(TextBoxAddress1.Text,
        TextBoxAddress2.Text, TextBoxCity.Text, TextBoxState.Text,
        TextBoxPostalCode.Text));
    }
}

The '< >'s indicate that EventHandler is a generic type. “ <>”表示EventHandler是通用类型。 If you have never been exposed to generics before I suggest you read this article: 如果您从未接触过泛型,建议您阅读这篇文章:

An Introduction to C# Generics C#泛型简介

Basically, you can think of a generic as a special class that you have the ability specify the type of object you want to store in it / have it be applicable to. 基本上,您可以将泛型视为一个特殊类,您可以指定要存储在其中的对象的类型/使其适用。 Once you do that, the methods and interaction with the class is strongly typed, so you do not need to cast things, or be forced to store them as an object type. 完成此操作后,将强类型化方法和与类的交互,因此您无需强制转换或将其存储为object类型。

A very commonly used generic is the List<T> type. 最常用的泛型是List<T>类型。 It is similar to an array, but has a flexible size (you don't need to re-declare its size) and you have convenience methods like Add() , Remove() , RemoveAt() , etc. So a List<int> could store however many integers you want, and a List<MyClass> object would be used for storing a collection of your MyClass objects. 它与数组相似,但是具有灵活的大小(您无需重新声明其大小),并且具有便捷的方法,如Add()Remove()RemoveAt()等。因此, List<int>可以存储所需的许多整数,并且List<MyClass>对象将用于存储MyClass对象的集合。

This syntax is called "generics". 此语法称为“泛型”。 Take a look at http://msdn.microsoft.com/en-us/library/ms379564(VS.80).aspx for an intro to the concept. 请访问http://msdn.microsoft.com/zh-cn/library/ms379564(VS.80).aspx ,以了解该概念。

In this specific case, it's used to say that this event will use the AddressEventArgs class for passing details into handlers. 在这种情况下,通常会说此事件将使用AddressEventArgs类将详细信息传递给处理程序。

If you're familiar with the old .NET event syntax (before generics existed), take a look at http://codebetter.com/jpboodhoo/2007/07/11/leveraging-the-eventhandler-lt-t-gt-delegate-more-effectively for a comparison 如果您熟悉旧的.NET事件语法(在存在泛型之前),请查看http://codebetter.com/jpboodhoo/2007/07/11/leveraging-the-eventhandler-lt-t-gt-更有效地委托进行比较

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM