泛型方法如何工作? [英] How does a Generic Method Work?

查看:110
本文介绍了泛型方法如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在MSDN上选择了这个教程,它允许我的应用程序用户更改和保存设置。经过一些头部搔抓之后,我想我明白了所有这一切,除了一件事。让我们从原始代码的最相关部分开始。一切都站在一个名为AppSettings的类中。我用来测试这个属性是son1。我的理解是这样的:



在代码的最后,你得到了这个:

  //属性获取并设置son1 Key。 
public int son1
{
get
{
return GetValueOrDefault< int>(son1KeyName,son1Default);

set
{
if(AddOrUpdateValue(son1KeyName,value))
{
Save();





如果你设置了属性,这很容易。它只是调用这个方法:

pre $ public bool AddOrUpdateValue(string Key,Object value)
{
bool valueChanged = false;
//如果密钥存在
if(settings.Contains(Key))
{
//如果值已经改变
if(settings [Key]! = value)
{
//存储新值
settings [Key] = value;
valueChanged = true;
}
}
//否则创建密钥。
else
{
settings.Add(Key,value);
valueChanged = true;
}
返回valueChanged;
}

我只需要这样做,我就完成了:

  AppSettings param = new AppSettings(); 
param.son1 = 1;

现在GET事件的语法对我来说似乎更加奇怪。过程是一样的,一个方法被一个属性使用。 get属性列在我的文章顶部。正如可以看到,它调用此方法:

 公共ŤGetValueOrDefault< T>(字符串键,T默认值)
{
T值;

//如果密钥存在,请检索该值。
if(settings.Contains(Key))
{
value =(T)settings [Key];
}
//否则,使用默认值。
else
{
value = defaultValue;
}
返回值;
}

我被T打扰了,它也写在< ;和>。如果我能理解它,我应该能够将该属性设置为默认值,在我的程序开始时我会做出这些默认值。任何提示表示赞赏。谢谢。

解决方案

这是泛型

这是一个复杂的主题,但它有几句话: it基本上意味着你的方法可以用于多种类型。为了简化,你可以用任何类型来替换你的T. 而不是写多个函数:

  public string GetValueOrDefault字符串键,字符串默认值)
公众诠释GetValueOrDefault(字符串键,诠释默认值)

您只写之一:

  T GetValueOrDefault< T>(字符串键,T默认值)


I recently picked up this tutorial on MSDN that allow the user of my app to change and save settings. After some head scratching I think I understand all of it, except for one thing. Let's start with the most relevant part of the original code. Everything stands in a class called AppSettings. The property I'm using to test this is son1. What I understand is this:

At the end of the code, you got this:

 // Property to get and set son1 Key.
    public int son1
    {
        get
        {
            return GetValueOrDefault<int>(son1KeyName, son1Default);
        }
        set
        {
            if (AddOrUpdateValue(son1KeyName, value))
            {
                Save();
            }
        }
    }

If you set the property, it's easy. It just calls this method:

public bool AddOrUpdateValue(string Key, Object value)
    {
        bool valueChanged = false;
 // If the key exists
        if (settings.Contains(Key))
        {
            // If the value has changed
            if (settings[Key] != value)
            {
                // Store the new value
                settings[Key] = value;
                valueChanged = true;
            }
        }
        // Otherwise create the key.
        else
        {
            settings.Add(Key, value);
            valueChanged = true;
        }
        return valueChanged;
    }

I just have to do that and I'm done:

AppSettings param = new AppSettings();
        param.son1 = 1;

Now the syntax for the GET thing seems more strange to me . Process is the same, a method is used by a property. The get property is listed at the top of my post. As you can see, it calls this method:

public T GetValueOrDefault<T>(string Key, T defaultValue)
    {
        T value;

        // If the key exists, retrieve the value.
        if (settings.Contains(Key))
        {
            value = (T)settings[Key];
        }
        // Otherwise, use the default value.
        else
        {
            value = defaultValue;
        }
        return value;
    }

I'm disturbed by the "T", which is also written between < and >. If I could understand it, I should be able to set the property to the default value, wich I'll propably do at the beginning of my program. any tip is appreciated. Thank you.

解决方案

It's the syntax for generics.

This is a complex subject, but it a few words: it basically means your method can be used with multiple types. To simplify, you can replace your T by any type. Instead of writing multiple functions:

public string GetValueOrDefault(string Key, string defaultValue)
public int GetValueOrDefault(string Key, int defaultValue)

you only write one:

T GetValueOrDefault<T>(string Key, T defaultValue)

这篇关于泛型方法如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆