c#在方法参数内发送运算符 [英] c# Send operators inside method paramers

查看:120
本文介绍了c#在方法参数内发送运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c#


是否可以在方法参数中发送'<'或'>'等运算符?



Public void change_color(Operator lessThan,Operator lessThan){



if(img.GetPixel(x,y).R lessThan 0 && img.GetPixel(x ,y).G MoreThan 128 && img.GetPixel(x,y).B LessThan 110)

{

//做点什么

}



}

解决方案

不是你想样的方式,不是。



您最好只使用适当的操作员创建第二种方法。



其他方法是可能但是他们会在你已经编写的糟糕代码之上引入性能命中。



Get / SetPixel执行一次非常慢而且你得到的确切相同的像素三次而不是一次。将一个解释的运算符放入混合中,你可以编写最慢的代码来执行此操作。



在此处搜索文章假人的图像处理 [ ^ ]你会得到一些关于如何在不使用GetPixel的情况下进行像素工作的文章,与你写的相比它会非常快。


< blockquote>你需要理解一个运算符可以被认为是方法语法糖。例如,您正在谈论二元运算符;从功能上讲,它们相当于一个函数接受两个参数(每个参数代表一个操作符)并返回该值。在您的情况下,他们返回布尔值。因此,您的参数应该是委托实例。比方说,对于 int 操作数,委托类型为委托bool比较(int left,int right)



但你不需要这样做。您已经拥有泛型类型 System.Func< int,int,bool> 这样的声明。此外,您可以使自己的类通用并使用此通用委托;这样,您可以从要比较的操作数类型中抽象出来。首先,让我们考虑一下非泛型类的方法。它可能类似于

 使用系统; 

// ...

< span class =code-keyword> public void ChangeColor(
Func< int, int ,bool> lessThen,
Func< int, int ,bool> greaterOrEqualThen / * ... * / ){
// ...
if (lessThen!= null ){
if (lessThen(myPixel, 0 ))
DoDomething();
else
DoSomethingElse();
} else
DoSomethingByDefault();
// ...
}

// ...

// 以下是调用它的方法:
ChangeColor(
new Func< int, int ,bool>((a,b)= > { 返回 a < b;}),
new Func< int, int ,bool>((a,b)= > {返回 a > = b;}));



我知道这种电话语法不是你想要的那么简单,但这就是你所拥有的。



以通用的方式,你可以有类似
的东西

 使用系统; 

// ...

< span class =code-keyword> class GraphicProcessing< OPERAND> {
public void ChangeColor(
Func< OPERAND,OPERAND,bool> lessThen ,
Func< OPERAND,OPERAND,bool> greaterOrEqualThen / * ... * / ) {
OPERAND myPixel = // ...
OPERAND compareTo = // ...
// ...
if (lessThen!= null ){
if (lessThen(myPixel,compareTo))
DoDomething();
else
DoSomethingElse();
} else
DoSomethingByDefault();
// ...
}
}





另请注意,您尝试使用非常慢的API, GetPixel ,速度过慢。如果你的意思是使用 System.Drawing ,可以使用 System.Drawing.Bitmap.LockBits 来有效地完成这些工作:

位图。 LockBits方法(System.Drawing) [ ^ ]。



您可以在MDSN帮助页面上找到信息丰富的代码示例上面引用的 LockBits 方法之一。



-SA


c#
Is it possible to send operators such as '<' or '>' in method parameters?

Public void change_color(Operator lessThan, Operator lessThan ){

if(img.GetPixel(x, y).R lessThan 0 && img.GetPixel(x, y).G MoreThan 128 && img.GetPixel(x, y).B LessThan 110)
{
//do something
}

}

解决方案

Not the way you're sample is thinking, no.

You'd be better off just creating a second method with the appropriate operators in place.

Other methods are possible but they introduce performance hits on top of the bad code you're already writing.

Get/SetPixel are VERY slow to execute once and you're getting the exact same pixel three times instead of once. Putting an interpreted operator into the mix and you've got the slowest possible code you could write to do this.

Search the articles here for "Image processing for dummies[^]" and you'll come up with a bunch of articles on how to do pixel work without using GetPixel and it'll be extremely fast compared to what you're writing.


You need to understand that an operator can be considered as syntactic sugar over the concept of method. For example, you are talking about binary operators; functionally, they are equivalent to a function accepting two arguments (each representing an operant) and returning the value. In your case, they return Boolean. Therefore, your arguments should be delegate instances. Say, for int operands, the delegate type is delegate bool Comparison(int left, int right).

But you don't need to do even that. You already have such declaration as generic type System.Func<int, int, bool>. Moreover, you can make your own class generic and use this generic delegate; this way, you can abstract from the types of your operands to be compared. First, let's consider the method of your non-generic class. It could be something like

using System;

// ...

public void ChangeColor(
        Func<int, int, bool> lessThen,
        Func<int, int, bool> greaterOrEqualThen /* ... */) {
   // ...
   if (lessThen != null) {
        if (lessThen(myPixel, 0))
            DoDomething();
        else
            DoSomethingElse();
    } else
        DoSomethingByDefault();                
   // ...
}

// ...

// Here is how you can call it:
ChangeColor(
   new Func<int, int, bool>((a, b) => { return a < b; }),
   new Func<int, int, bool>((a, b) => { return a >= b; }));


I understand that this syntax of call is not as simple as you wanted, but this is what you have.

To to it in generic way, you can have something like

using System;

// ...

class GraphicProcessing<OPERAND> {
    public void ChangeColor(
            Func<OPERAND, OPERAND, bool> lessThen,
            Func<OPERAND, OPERAND, bool> greaterOrEqualThen /* ... */) {
        OPERAND myPixel = //...
        OPERAND compareTo = //...
        // ...
        if (lessThen != null) {
            if (lessThen(myPixel, compareTo))
                DoDomething();
            else
                DoSomethingElse();
        } else
            DoSomethingByDefault();                
        // ...
    }
}



Also note that you are trying to use very slow API, GetPixel, prohibitively slow. If you mean to use System.Drawing, such things can be done efficiently using System.Drawing.Bitmap.LockBits:
Bitmap.LockBits Method (System.Drawing)[^].

You will find a very informative code sample on the MDSN help page on one of the LockBits methods referenced above.

—SA


这篇关于c#在方法参数内发送运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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