在C#中重载性能 [英] Overloading properties in C#

查看:208
本文介绍了在C#中重载性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我知道超载财产没有在C#中的支持 - 大多数引用援引单的方法,不同的,返回类型问题的解释。然而,关于制定者?我想直接分配一个值作为字符串或者对象,但只返回一个字符串

Ok, I know that property overloading is not supported in C# - most of the references explain it by citing the single-method-different-returntype problem. However, what about setters? I'd like to directly assign a value as either a string or object, but only return as a string.

这样的:

    public string FieldIdList
    {
        get { return fieldIdList.ToString(); }
        set { fieldIdList = new FieldIdList(value); }
    }

    public FieldIdList FieldIdList 
    {
        set { fieldIdList = value; }
    }
    private FieldIdList fieldIdList;



为什么不这样被允许?我还看到,属性只需编译创建的getter / setter函数。是否有可能创建自己的?是这样的:

Why wouldn't this be allowed? I've also seen that "properties" simply create getter/setter functions on compile. Would it be possible to create my own? Something like:

    public void set_FieldIdList(FieldIdList value)
    {
        fieldIdList = value;
    }

这会做同样的事情。思考?

That would do the same thing. Thoughts?

推荐答案

属性其实都是一对get / set方法(其中一个可能会被排除在外),但物业本身有这使得它摆在首位,而不是只有两个方法,一个属性的其他元数据。

Properties are in fact a pair of get/set methods (one of which may be left out), but the property itself has additional metadata which makes it a property in the first place instead of just two methods.

由于属性的签名仍然是只占-不同按返回无效型,即使你只有一个二传手。如果你需要这个,不要使用财产;设置属性只有反正不是一件好事。

Because the property signature remains invalid as per only-different-by-return-type, even if you only have a setter. If you need this, don't use a property; set-only properties aren't a good thing anyways.

这篇关于在C#中重载性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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