문제

Having even more than two options to choose from, leads me to question, which one to choose, if the result / outcome is same.

in .NET C# the following conditions are asking same question using different operators, so the question is , what experienced developers use, i tend to assume that ready made operators like Equals would go through more processing actions.

When and why would you choose ! over Equals, and both over 'traditional' == ?

//bool
if (!Page.IsPostBack) 
{
    //bool
    if (NotAuthorized().Equals(false)) 
    {            
        AppsCtrls.DDLs_Init();

        //bool
        if (CurrSeSn.Raised(Flag.MainDataSet_IsPopulated) == false)
        {
            initALLDataSet(AllDataStColsSelectionMod.doneViaSP);
        }

        custid = RConv.Str2int(Request.QueryString["custid"]);
        username = GetTableData.AsString("name", "tblCustomers", "custid", custid);
    }
}
도움이 되었습니까?

해결책

This question is somewhat subjective...

"i tend to assume that ready made operators like Equals would go through more processing actions."

" when and why would you choose ! over Equals , And both over 'traditional' == "

the Equals method as part of an instance of an object is used to check for equality of that instance against another, whilst the == and != operators are static and are therefore unbound from any object instance. Instead these are like a special static method that accepts two arguments (of usually the same type) and compares them.

Consider the following example:

public class CustomObject
{
    int someValue, anotherValue;    

    public bool Equals(CustomObject obj)
    {
        return (this.someValue == obj.someValue && this.anotherValue == obj.anotherValue);
    }

    public static bool operator ==(CustomObject a, CustomObject b)
    {
        return a.Equals(b);
    }

    public static bool operator !=(CustomObject a, CustomObject b)
    {
        return !(a == b);
    }
}

In this example, the Equals method is used to produce a result of the comparison of values in the CustomObject against another instance of the same type. The == operator for CustomObject simply calls Equals on one of the parameter objects and performs an equality check against the other. The != operator simply negates the == and produces the opposite result. Therefore, == and != do not have much performance overhead over Equals, because they both call that method anyway.

Best practices:

if a condition is boolean by nature there is no need to use Equals, != or ==, but you should use ! to negate a boolean condition.

For example:

if(IsPostBack) // this is good

if(IsPostBack == true) // this is unnecessary

if(!IsPostBack) // this is good

if(IsPostBack == false) // this is unnecessary

If a condition is not boolean by nature, or you are comparing two boolean values, or you are comparing an enumeration, or other value type then use of != or == is acceptable.

For example:

if(a == b) // this is good

if(a != b) // this is good

If a condition is not boolean by nature and the objects you are comparing do not have the == or != operators implemented, then using Equals is acceptable. Note, using != and == are prohibited with generics since it is not known at compile time that != or == are implemented on the objects represented by the generic objects type parameters.

For example:

if(a.Equals(b)) //this is good

if(!a.Equals(b)) // this is good

if(a.Equals(b) == true) // this is unnecessary

다른 팁

I typically choose the option that is the shortest, ie:

if (!Page.IsPostBack)

Or:

if (Authorized())

Since C# doesn't allow non-boolean expressions in an if statement, this is perfectly clear, so I see no reason for the extra typing.

That being said, this is really purely a matter of convention and preference - there is no performance advantage to using one form over the other.

This is different than C++, for example, where you can use if (42), in which case seeing if (foo) isn't enough to know whether foo is a boolean, or some other type. In that scenario, it occasionally makes sense to include the condition check (ie: if (foo == false)) as you can then see the type distinctly and make your intentions clear.

Use the first. Unlike C/C++ where zero/null == false, in C# only boolean value types can be used with the boolean operators, i.e.:

int i=1;
if (i) { .... }

Will not compile, so there is no need to explicitly test equality of booleans, specially when using descriptive names (IsSomething, HasHappened, NotSomething, etc.)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top