Check if at least one object is not zero

Dan*_*tta 2 c#

This function need to check that a=b=c=0 or only one is <>0

there is a best way to do this check?

void (int p, int a, int s) {
    if ((p != 0 && a != 0) 
        || (p != 0 && s != 0)
        || (a != 0 && s != 0)
        || (a != 0 && s != 0 && p != 0)) throw new Exception("Error please set A or P or S");

}
Run Code Online (Sandbox Code Playgroud)

Pet*_*r B 5

一个简单的解决方案,无需使用 Linq 或任何随之而来的开销:

public void Check(int p, int a, int s)
{
    var count = 0;
    if (p != 0) count++;
    if (a != 0) count++;
    if (s != 0) count++;
    if (count >= 2)
        Console.WriteLine("Please set only A or P or S or none");
    else
        Console.WriteLine("OK");
}
Run Code Online (Sandbox Code Playgroud)

工作小提琴: https: //dotnetfiddle.net/ViMmRV