Tag: 函数

有没有办法捕获lambda表达式,以便它不是编译时强制将身份作为表达式或委托类型?

假设我有一个复杂的lambda表达式,如下所示: x => xAHasValue || (xBHasValue && xC == q) || (!xCHasValue && !xAHasValue) || //…expression goes on 我想将它用作Expression<Func in(例如Linq-To-Entities) Queryable.Where方法。 我也想在Enumerable.Where方法中使用它,但Where方法只接受Func ,而不是Expression<Func 。 lambda语法本身可用于生成Expression<Func>或Func (或任何委托类型),但在此上下文中,它不能一次生成多个。 例如,我可以写: public Expression<Func> PairMatchesExpression() { return x => xA == xB; } 就像我写的那样容易: public Func PairMatchesDelegate() { return x => xA == xB; } 问题是我无法以两种方式使用相同的lambda表达式(即x => xA == xB),而无需将其物理地复制到具有两种不同返回类型的两个单独方法中,尽管编译器能够将其编译为任意一个。 […]

C#:循环查找函数的最小值

我目前有这个function: public double Max(double[] x, double[] y) { //Get min and max of x array as integer int xMin = Convert.ToInt32(x.Min()); int xMax = Convert.ToInt32(x.Max()); // Generate a list of x values for input to Lagrange double i = 2; double xOld = Lagrange(xMin,x,y); double xNew = xMax; do { xOld = xNew; xNew = […]

function别名

我想从kernel32.dll导入一些函数,但我想使用不同的名称。 function示例: [DllImport(“kernel32.dll”)] private static extern bool ReadProcessMemoryProc64 (…); private static bool BetterReadableAndWriteableName (…) { ReadProcessMemoryProc64(…); } 如果有另一种方式,包装该function是我实际上不想要的。

构造函数链接中间变量

我有以下的重载构造函数,我正在努力找到一个很好的解决方案。 我看不到如何使用构造函数链接的中间赋值。 以下内容无效,但显示了我想要做的事情 public MyThing(IServiceLocator services, int? userId) { // blah…. } public MyThing(IServiceLocator services, string userName) { User user = services.UserService.GetUserByName(userName); int userId = user == null ? null : (int?)user.Id; // call the other constructor this(services, userId); } 我知道在有效代码中编写上述内容的唯一方法是 public MyThing(IServiceLocator services, string userName) : this(services, services.UserService.GetUserByName(userName) == null ? null : (int?)services.UserService.GetUserByName(userName).Id) […]

对象是否应返回null并且对象集合返回空集合?

今天出现了返回空值,空对象和空集合的主题,我们希望得到其他人的意见。 我们已经阅读了关于为对象返回null的讨论以及关于返回空集合的讨论并且同意两个主题的标记答案。 但是,在我们的例子中,我们有一个包含Foo.Item类和Foo.Items类的类,它是Foo.Item对象的集合。 如果Foo.Item返回null,Foo.Items集合是否也返回null,还是应该返回空集合?

如何将csharp绑定变量传递给Javascript函数

我想将c#绑定变量传递给javascript函数。 这是我的代码: <asp:LinkButton ID="lbID" runat="server" Text="” OnClientClick=”passAccessory(”); “> 但它总是会产生错误:“htmlfile:未实现”。 并且代码没有被解释为onclick=”passAccessory(‘<%#Bind(variable)%>’);” 有谁知道如何解决它? 谢谢。 编辑:我已将代码更改为 <asp:LinkButton ID="lbID" runat="server" Text="” OnClientClick=”passAccessory(”);”> 这是与它相关的代码,它使用lblTest.ClientID <asp:Label ID="lblTest" runat="server" Text='’ /> 但我收到一个错误: HttpException. Databinding … does not contain a property with the name lblTest HttpException. Databinding … does not contain a property with the name lblTest 。 我的代码有什么问题吗? 编辑:这些post给了我一个线索。 http://www.west-wind.com/Weblog/posts/5364.aspx 我试图学习如何将IEnumerable LINQ集合绑定到转发器 […]

从C#调用C ++ DLL函数时出现问题

这是关于C#中的一个河豚问题的第三个主题。虽然我不能在我的应用程序中实现blowfish,但我决定将它用作外部C ++ DLL。 请注意我已经尝试过Blowfish.NET和其他任何一个,问题是我正在将代码从C ++转换为C#,而C#代码必须与C ++代码完全相同。 至今: —> C ++ DLL源码<— 请注意,导出的函数位于代码的末尾 C#代码(定义) [DllImport(“TestDLL.dll”, EntryPoint = “Initkey” ,ExactSpelling = true , CallingConvention = CallingConvention.Cdecl)] public static unsafe extern void Initkey(byte[] key); [DllImport(“TestDLL.dll”, EntryPoint = “encode”, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)] public static unsafe extern void encode(UInt32 *stream); C#代码(函数调用) – 初始化河豚钥匙 UInt32[] keyarray = new […]

如何圆到一半,总是在积极的方向?

我如何实现以下四舍五入? 0.012608376> 0.015 2.1> 2.5 2.4> 2.5 2.5> 2.5 2.6> 3 .01> .05

获取对象调用层次结构

可以说我有3节课: class A { void do_A() { //Check object call hierarchy } } class B { void do_B() { A a; a.do_A(); } } class C { void do_C() { B b; b.do_A(); } } 然后我打电话给: C c; c.do_C(); 如何从A的do_A()中获取对象调用层次结构? 我的意思是我想在a.do_A()中获取对象的引用(可以通过this方便地实现),对象b的引用调用a.do_A() ,以及对象c的引用,称为b.do_B( ) 。 我认为这应该是可能的,因为我可以通过调用堆栈获得调用层次结构,所以我确信我应该能够获得有关调用这些方法的对象的更多信息。

function级别范围和块级别范围之间的差异

我用JavaScript编程了几个月,主要使用jQuery 。 我理解闭包并且我已经使用过它们,但是,我仍然无法理解其他语言(如C#)中 函数级别范围和块级别范围之间的区别 。 我一直在努力教自己,没有关于这个问题的结果。 有人可以用一些简单的例子来解释我吗?