[ Pobierz całość w formacie PDF ]
.The conditional operator can be used for its side effects or for the value itproduces, but in general you want the value since that’s what makes theoperator distinct from the if-else.Here’s an example:static int Ternary(int i) {return i < 10 ? i * 100 : i * 10;}You can see that this code is more compact than what you’d need to writewithout the ternary operator:static int Alternative(int i) {if (i < 10)return i * 100;elsereturn i * 10;}The second form is easier to understand, and doesn’t require a lot more typing.So be sure to ponder your reasons when choosing the ternary operator – it’sgenerally warranted when you’re setting a variable to one of two values:int ternaryResult = i < 10 ? i * 100 : i * 10;The comma operatorThe comma is used in C and C++ not only as a separator in function argumentlists, but also as an operator for sequential evaluation.The sole place thatthe comma operator is used in C# is in for loops, which will be described laterin this chapter.@Todo: Confirm that this is all the operators in C#Common pitfalls when using operatorsOne of the pitfalls when using operators is trying to get away withoutparentheses when you are even the least bit uncertain about how an expressionwill evaluate.This is still true in C#.An extremely common error in C and C++ looks like this:while(x = y) {//.}The programmer was trying to test for equivalence (==) rather than do anassignment.In C and C++ the result of this assignment will always be true if yis nonzero, and you’ll probably get an infinite loop.In C#, the result of thisexpression is not a bool, and the compiler expects a bool and won’t convertfrom an int, so it will conveniently give you a compile-time error and catchthe problem before you ever try to run the program.So the pitfall neverhappens in C#.(The only time you won’t get a compile-time error is when x andy are bool, in which case x = y is a legal expression, and in the above case,probably an error.)A similar problem in C and C++ is using bitwise AND and OR instead of thelogical versions.Bitwise AND and OR use one of the characters (& or |) whilelogical AND and OR use two (&& and ||).Just as with = and ==, it’s easy totype just one character instead of two.In C#, the compiler again prevents thisbecause it won’t let you cavalierly use one type where it doesn’t belong.Casting operatorsThe word cast is used in the sense of “casting into a mold.” C# willautomatically change one type of data into another when appropriate.Forinstance, if you assign an integral value to a floating-point variable, thecompiler will automatically convert the int to a float.Casting allows you tomake this type conversion explicit, or to force it when it wouldn’t normallyhappen.To perform a cast, put the desired data type (including all modifiers) insideparentheses to the left of any value.Here’s an example:void casts() {int i = 200;long l = (long)i;long l2 = (long)200;}As you can see, it’s possible to perform a cast on a numeric value as well ason a variable.In both casts shown here, however, the cast is superfluous,since the compiler will automatically promote an int value to a long whennecessary.However, you are allowed to use superfluous casts to make a point orto make your code more clear.In other situations, a cast may be essential justto get the code to compile.In C and C++, casting can cause some headaches.In C#, casting is safe, withthe exception that when you perform a so-called narrowing conversion (that is,when you go from a data type that can hold more information to one that doesn’thold as much) you run the risk of losing information.Here the compiler forcesyou to do a cast, in effect saying “this can be a dangerous thing to do—if youwant me to do it anyway you must make the cast explicit.” With a wideningconversion an explicit cast is not needed because the new type will more thanhold the information from the old type so that no information is ever lost.Java allows you to cast any primitive type to any other primitive type, exceptfor bool, which doesn’t allow any casting at all.Class types do not allowcasting.To convert one to the other there must be special methods [ Pobierz caÅ‚ość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • orla.opx.pl