recently i have been reading a tutorial book and came across a question mark in the script and wondered what it did the script stops working when i take it out but works when it's in here is where it occurs any idea of what it does is better than what I know at the moment
Its a http://en.wikipedia.org/wiki/Ternary_operation, you could write this statement like this: Code (csharp): if (Input.GetAxis("Horizontal") <0) in_direction = -1; else in_direction = 1;
It is a short hand for writing an if statement. That code is equivalent to: Code (csharp): if (Input.GetAxis("Horizontal") < 0) in_direction = -1 else in_direction = 1; Edit: haha, 3 identical replies in under a minute
Got to advise against this behaviour, all it really does is lead to bugs. Keep the code clean, people. It isn't faster to execute on mono.
It's the conditional operator. Full documentation on it can be found here. In short, it evaluates whatever is to the left of the question mark, which in this case is Code (csharp): Input.GetAxis ("Horizontal") < 0 So if that's true (eg, the player is holding left on the keyboard/joystick), the resulting value is what's immediately to the right of the question mark (in this case, -1). Otherwise (player input is neutral or to the right), it's whatever's to the right of the colon (or 1, in your example). It would probably be clearer if the code was spaced a little differently, but that's the gist of what it does. You can also chain them together, so if, for example, you wanted to exclude the neutral input case, you could do: Code (csharp): Input.GetAxis ("Horizontal") < 0 ? -1 : (Input.GetAxis ("Horizontal") > 0 ? 1 : 0)
The usage of it alone doesn't lead to bugs, however any bugs that arise out of it are more difficult to debug because you can't step through it like you can an if-else block. While I wouldn't typically use it for something like this, I will use it periodically in a get for boolean properties.
Yeah, actually. I'll say it in a way you can understand: Usage of this makes your code piss poor to read. Happy?
It can be useful for checking for nulls in a concise way: string Firstname = person["First Name"] != null ? person["First Name"].ToString() : String.Empty;
Actually it is slightly faster in some cases (not enough to be worth caring about though). Also, in some cases I do consider the code to be cleaner, such as when declaring variables. e.g., Code (csharp): var foo = 5; if (something > 42) { foo = 8; } compared to Code (csharp): var foo = (something > 42)? 8 : 5; I also use it when the alternative involves cumbersome repetition of code: Code (csharp): if (something) { blah = AComplicatedFunction (12, Vector3.zero, foo, 85, theKitchenSink, true); } else { blah = AComplicatedFunction (12, Vector3.zero, foo, 85, theKitchenSink, false); } compared to Code (csharp): blah = AComplicatedFunction (12, Vector3.zero, foo, 85, theKitchenSink, something? true : false); Especially in cases like this, where the focus should be on assigning the result of AComplicatedFunction to "blah", not using the ternary operator changes it to a whole ordeal about checking "something", which is really just one parameter out of several, plus checking "something" becomes physically separated from the results, so you have to scan multiple lines and play "spot the difference" to figure out what's going on. Otherwise, I would agree that using it to make cute one-line statements for the sake of it is a bad idea. Like the example above, Code (csharp): Input.GetAxis ("Horizontal") < 0 ? -1 : (Input.GetAxis ("Horizontal") > 0 ? 1 : 0) I would definitely consider that obfuscated. --Eric
Try this: Code (csharp): var Firstname = person["First Name"] ?? string.Empty; Only works when person["bla"] returns the same type as the right value though.
I prefer it as opposed to a full on if else block. Much cleaner IMO, and not that much harder to read unless you're an amateur. Nested ones can get tricky to read tho... myvalue = value > 5 ? 1 : value > 10 ? 2 : 0;
The ?? returns what's to the left of it, if that isn't null, or what's on the right otherwise. So, if person["First Name"] returned a valid string, Firstname would be set to whatever person["First Name"] would evaluate to. Otherwise, it would end up being set to string.Empty. It's a little bit like the ? one that was discussed above except that it is specifically checking for null. Another way to write it is: Code (csharp): var Firstname; if (person["First Name"] != null) { Firstname = person["First Name"]; } else { Firstname = string.Empty; }
The Docs for "??": http://msdn.microsoft.com/en-us/library/ms173224.aspx Very handy for nullable types, also nice shorthand syntax for reference types. Edit: Also, I prefer the ternary operator over if-else for legibility when it's simple assignations(with the same reasoning as Eric5h5), rather than more complex logic. The problem of debugging arises when people do stupid messes like this: Code (csharp): int a = b ?? c ? d : e ? f ? g : h : i; That kinda of coding should be taken out back and shot. Though there is nothing difficult to read or debug from a simple use of ternary operator. There is also a slight performance boost, as it's sometimes called the 'Branch-less Conditional', but only under some conditions(e.g. don't call functions with it). Though it's a very micro-optimization, so 99.9%+ of the time you don't choose it for it's speed.
that has cleared lots up thanks for the detail as well everyone and one more what does "!" do it comes up sometimes
Not. Code (csharp): bool thing; // if thing is true if (thing) {...} // if thing is false (or 'NOT' true) if (!thing) {...}