默认参数仍为空字符串。

huangapple go评论97阅读模式
英文:

Default parameter is still empty string

问题

我有一个继承自接口的类,定义如下:

  1. public interface IModel
  2. {
  3. List<KeyValuePair<string, string>> AddValuePairs(string prefix = "");
  4. }

一切都很好,但是当我调用这个方法并传递一个前缀时,它仍然将其视为空字符串。

  1. public sealed class AuthURLInputModel : IModel
  2. {
  3. public string Username {get;set;}
  4. public string Idnumber {get;set;}
  5. public List<KeyValuePair<string,string>> AddValuePairs(string prefix="user")
  6. {
  7. var pre = !string.IsNullOrEmpty(prefix) ? prefix : string.Empty;
  8. var keyValuePairs = new List<KeyValuePair<string, string>>
  9. {
  10. new KeyValuePair<string, string>(ModelHelper.GetPrefixedName("username", prefix), Username),
  11. new KeyValuePair<string, string>(ModelHelper.GetPrefixedName("idnumber", prefix), Idnumber)
  12. };
  13. return keyValuePairs;
  14. }
  15. }

调用

AddValuePairs(string prefix = "user[0]")

仍然将前缀视为空字符串,而不是传入的

"user[0]"

有什么建议吗?

英文:

I have a class that inherits from Interface with this definition

  1. public interface IModel
  2. {
  3. List&lt;KeyValuePair&lt;string, string&gt;&gt; AddValuePairs(string prefix = &quot;&quot;);
  4. }

All is fine but when I call the method and pass a prefix it still sees it as empty.

  1. public sealed class AuthURLInputModel : IModel
  2. {
  3. public string Username {get;set;}
  4. public string Idnumber {get;set;}
  5. public List&lt;KeyValuePair&lt;string,string&gt;&gt; AddValuePairs(string prefix=&quot;user&quot;)
  6. {
  7. var pre = !string.IsNullOrEmpty(prefix) ? prefix : string.Empty;
  8. var keyValuePairs = new List&lt;KeyValuePair&lt;string, string&gt;&gt;
  9. {
  10. new KeyValuePair&lt;string, string&gt;(ModelHelper.GetPrefixedName(&quot;username&quot;, prefix), Username),
  11. new KeyValuePair&lt;string, string&gt;(ModelHelper.GetPrefixedName(&quot;idnumber&quot;, prefix), Idnumber)
  12. };
  13. return keyValuePairs;
  14. }
  15. }

Calls to

> AddValuePairs(string prefix = "user[0]")

still sees prefix as empty string instead of

> "user[0]"

that was passed in.

  1. protected TModel Post&lt;TModel, TInputModel&gt;(string funcName, TInputModel inputModel)
  2. where TInputModel : IModel
  3. {
  4. var inputs = inputModel.AddValuePairs();
  5. }

Where TInputModel is AuthURLInputModel
Any suggestion?

答案1

得分: 3

可选参数的默认值是在编译时根据方法调用目标表达式的编译时类型使用的。在这种情况下,编译时类型是TInputModel,但编译器只“知道”接口中的声明,所以它确定要使用哪个默认值。

这里有一个更简单的完整示例:

  1. using System;
  2. public interface IFoo
  3. {
  4. void M(int x = 0);
  5. }
  6. public class Foo : IFoo
  7. {
  8. public void M(int x = 10)
  9. {
  10. Console.WriteLine(x);
  11. }
  12. }
  13. class Test
  14. {
  15. static void Main()
  16. {
  17. Foo foo1 = new Foo();
  18. IFoo foo2 = foo1;
  19. // 输出10,因为编译器知道Foo.M的声明
  20. foo1.M();
  21. // 输出0,因为编译器只知道IFoo.M的声明
  22. foo2.M();
  23. }
  24. }

我强烈建议您不要更改接口实现(或基类重写)方法中的默认值。这会导致混淆-正如您的问题所示。

英文:

The default value for optional parameters is used at compile-time, based on the compile-time type of the expression of the target of the method call. In this case, the compile-time type is TInputModel, but the compiler only "knows" about the declaration in the interface, so that's how it determines which value to use by default.

Here's a simpler, complete example:

  1. using System;
  2. public interface IFoo
  3. {
  4. void M(int x = 0);
  5. }
  6. public class Foo : IFoo
  7. {
  8. public void M(int x = 10)
  9. {
  10. Console.WriteLine(x);
  11. }
  12. }
  13. class Test
  14. {
  15. static void Main()
  16. {
  17. Foo foo1 = new Foo();
  18. IFoo foo2 = foo1;
  19. // Prints 10, because the compiler knows
  20. // about the declaration of Foo.M
  21. foo1.M();
  22. // Prints 0, because the compiler only
  23. // knows about the declaration of IFoo.M
  24. foo2.M();
  25. }
  26. }

I would strongly advise you not to change the default value in interface-implementing (or base-class-overriding) methods. It causes confusion - as your question demonstrates.

huangapple
  • 本文由 发表于 2023年8月8日 22:36:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860595.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定