构造函数重载包含已定义具有相同签名的成员 [英] Constructor overload contains already defines a member with the same signature

查看:56
本文介绍了构造函数重载包含已定义具有相同签名的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public Module(string a, object obj) : this(a, null, obj) { }

public Module(string b, object obj) : this(null, b, obj) { }

这些构造函数重载不起作用

These constructor overloads do not work

已经定义了具有相同参数类型的成员"

'already defines a member with same parameter types'

我环顾四周,意识到我无法在C#中做到这一点,但是任何人都可以提出解决此问题的方法吗?

I have looked around and realise that I cannot do this in c# but can anyone suggest a way around this?

谢谢您的回答.

在这种情况下,我决定暂时使用:

In this case I have decided that to go with this for now:

public Module(string a, object obj) : this(a, null, obj) { }

public Module(string a, string b, object obj) : this(a, b, obj) {}

因此,如果用户想使用b,则必须包括a ...不太好,但是您可以使用

So users will have to include a if they want to use b... not brilliant but there you go

推荐答案

在重载的情况下,参数名称毫无意义.我可以看到您要执行的操作,但不知道为什么.我会完全省掉它:

Parameter names are meaningless in the context of overloads. I can see what you are trying to do, but I'm not sure why. I would dispense with it entirely:

public Module(string a, string b, object obj){}

然后调用 Module 构造函数,并根据需要传入 null 值.

Then call the Module constructor, passing in null values as appropriate.

Module m = new Module(null, "hi", obj);

Module m2 = new Module("bye", null, obj);

这篇关于构造函数重载包含已定义具有相同签名的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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