Tag: 参数

为什么匿名委托可以省略参数,但lambdas不能?

//ok Action CallbackWithParam1 = delegate { }; //error CS1593: Delegate ‘System.Action’ does not take 0 arguments Action CallbackWithParam2 = () => { }; 只是想知道为什么差异真的。 : – /

在初始化惰性实例时,将参数传递给构造函数

public class myClass { public myClass(String InstanceName) { Name = InstanceName; } public String Name { get; set; } } // Now using myClass lazily I have: Lazy myLazy; Console.WriteLine(myLazy.Value.Name); 我的问题是当我们使用惰性实例时如何将InstanceName传递给myClass构造函数?

如何从c#中的MethodCallExpression调用该方法

我有一个方法调用表达式并尝试调用该方法。 我找到了一种方法,但是我在检索参数值方面遇到了问题,因为不是每个参数都用ConstantExpression描述。 Expression<Action> = t => t.DoSomething(Par0, Par1, Par2); MethodCallExpression methodCallExpression = selector.Body as MethodCallExpression; // get the information which is needed to invoke the method from the provided // lambda expression. MethodInfo methodInfo = methodCallExpression.Method; object[] arguments = methodCallExpression.Arguments.OfType() .Select(p => p.Value).ToArray(); // invoke the expression on every item within the enumerable foreach (TSource […]

参数的最佳实践:IEnumerable与IList对比IReadOnlyCollection

当得到延迟执行中的值时,我会从方法返回 IEnumerable时得到。 返回一个List或IList应该只是在修改结果的时候,否则我会返回一个IReadOnlyCollection ,所以调用者知道他得到的不是用于修改的(这使得该方法甚至可以重用对象)来自其他来电者)。 但是,在参数输入方面,我有点不太清楚。 我可以采用IEnumerable ,但如果我需要多次枚举怎么办? 俗话说“ 你发送的东西要保守,你接受的东西要自由 ”,建议拿一个IEnumerable是好的,但我不太确定。 例如,如果以下IEnumerable参数中没有元素,则可以通过首先检查.Any()来保存此方法中的大量工作,这需要在此之前使用ToList()以避免枚举两次 。 public IEnumerable RemoveHandledForDate(IEnumerable data, DateTime dateTime) { var dataList = data.ToList(); if (!dataList.Any()) { return dataList; } var handledDataIds = new HashSet( GetHandledDataForDate(dateTime) // Expensive database operation .Select(d => d.DataId) ); return dataList.Where(d => !handledDataIds.Contains(d.DataId)); } 所以我想知道什么是最好的签名,在这里? 一种可能性是IList data ,但接受列表表明您计划修改它,这是不正确的 – 此方法不会触及原始列表,因此IReadOnlyCollection似乎更好。 但是IReadOnlyCollection强制调用者每次执行ToList().AsReadOnly()都会变得有点难看,即使使用自定义扩展方法.AsReadOnlyCollection […]

如何将字符串参数传递给t4模板

嗨,我想找到一种方法将普通字符串作为参数传递给文本模板。 这是我的模板代码,如果有人能告诉我在c#中需要写什么来传递我的参数并创建类文件。 那将是非常有帮助的,谢谢。 namespace { using System; using System.Collections.Generic; using System.Linq; using System.Xml; /// /// This class describes the data layer related to . /// /// /// <change author=`Auto Generated` date=>Original Version /// public partial class : DataObject { #region constructor /// /// A constructor which allows the base constructor to attempt to extract the […]

可以使用预定义参数将方法附加到委托吗?

有时我会遇到需要将方法附加到委托但签名不匹配的情况,例如尝试将abc附加到somedelegate,字符串参数为“hi”。 public class test { //… public void abc(int i, string x) { //Do Something } //… } public class test2 { somedelegate x; //… public test2() { //Do Something test y = new test(); x += y.abc(,”hi”); } delegate void somedelegate(int i); } 我可以通过创建具有正确签名的另一个委托然后附加它来解决它,但它似乎不必要地复杂。 你能用C#做这样的事吗? 谢谢。 对。 我想我没有办法按照我的设想去做,但我仍然可以像x + =(int i)=> abc(i,“hi”);那样做。 所以现在没问题,谢谢你们。

使用OleDbParameter在Access中插入日期/时间值

我正在尝试在oledb(ms访问数据库)中进行插入,名为objectdate的字段是日期/时间 我用来添加参数的代码就是这个,但是我收到了错误。 OleDbParameter objectdate = new OleDbParameter(“@objectdate”, OleDbType.DBDate); objectdate.Value = DateTime.Now; cmd.Parameters.Add(objectdate); 错误: 条件表达式中的数据类型不匹配。

如何将匿名类型作为参数传递?

如何将匿名类型作为参数传递给其他函数? 考虑这个例子: var query = from employee in employees select new { Name = employee.Name, Id = employee.Id }; LogEmployees(query); 这里的变量query没有强类型。 我应该如何定义我的LogEmployees函数来接受它? public void LogEmployees (? list) { foreach (? item in list) { } } 换句话说,我应该使用什么而不是? 分数。

参数varbinary数据类型中的空值

如何在参数varbinary数据类型中添加空值? 当我执行以下代码时: using (SqlConnection myDatabaseConnection1 = new SqlConnection(myConnectionString.ConnectionString)) { using (SqlCommand mySqlCommand = new SqlCommand(“INSERT INTO Employee(EmpName, Image) Values(@EmpName, @Image)”, myDatabaseConnection1)) { mySqlCommand.Parameters.AddWithValue(“@EmpName”, textBoxEmpName.Text); mySqlCommand.Parameters.AddWithValue(“@Image”, DBNull.Value); myDatabaseConnection1.Open(); mySqlCommand.ExecuteNonQuery(); } } 我得到以下System.Data.SqlClient.SqlException : 不允许从数据类型nvarchar到varbinary(max)的隐式转换。 使用CONVERT函数运行此查询。

为什么Funcs不接受超过16个参数?

由于Javascript是我最熟练的语言,因此我熟悉将函数用作第一类对象。 我原以为C#缺少这个function,但后来我听说Func和Action以及delegate ,我认为这非常棒。 例如,您可以声明一个连接两个字符串的Func ,并在它们之间放置一个空格,如下所示: Func concat = (a,b) => a + ” ” + b; 当你输入时我注意到了 Func< IntelliSense显示它有17个重载: delegate System.Func delegate System.Func delegate System.Func …snip… delegate System.Func 那让我开怀大笑。 我查看了Func的MSDN文档并再次笑了起来。 这让我尝试用17个参数声明一个Func 。 它会导致错误( Using the generic type ‘System.Func’ requires 1 type arguments )。 我同意,拥有一个接受超过16个参数的Func可能不是一个好主意。 即便如此,这似乎是Func实施的一种方式。 它需要记录17个简单的不同重载。 这是它真正需要知道的全部内容:最后一个类型参数是返回类型,并且它之前的所有类型参数都是参数类型。 那么,如果我想创建一个超过16个参数的Func ,我该怎么办? 为什么还有限制? 为什么C#不能让你用任意数量的参数声明一个Func ?