Tag: optional arguments

可选参数和InteropServices

我正在使用第一次可选参数,但我无法理解这两个方法定义之间的区别: private void method1([Optional, DefaultParameterValue(string.Empty)] string testString) { //do something } private void method2(string testString = “”) { //do something } method1的定义需要: using System.Runtime.InteropServices; Method2定义较小,无需导入。 在使用其中一种方法语法之前,我是否要考虑一些事情?

在Python中创建一个C#Nullable Int32(使用Python.NET),使用可选的int参数调用C#方法

我正在使用Python.NET加载C#程序集以从Python调用C#代码。 这很干净,但是我遇到一个问题,调用一个如下所示的方法: Our.Namespace.Proj.MyRepo中的方法: OutputObject GetData(string user, int anID, int? anOptionalID= null) 我可以为存在可选的第三个参数的情况调用该方法,但是无法确定要为第三个参数传递什么以匹配null情况。 import clr clr.AddReference(“Our.Namespace.Proj”) import System from Our.Namespace.Proj import MyRepo _repo = MyRepo() _repo.GetData(‘me’, System.Int32(1), System.Int32(2)) # works! _repo.GetData(‘me’, System.Int32(1)) # fails! TypeError: No method matches given arguments _repo.GetData(‘me’, System.Int32(1), None) # fails! TypeError: No method matches given arguments iPython Notebook表明最后一个参数应该是类型: System.Nullable`1[System.Int32] 只是不确定如何创建一个与Null案例相匹配的对象。 有关如何创建C#识别的Null对象的任何建议? […]