How can I replace an if statement with a ternary operator? - c#

I am trying to replace the conventional if statement with ternary operator, but it does not compile.
First I would like to check if the input is a number, if true then assign Student.ID to input, if falsethen tell a user to try again.
My code:
(Int32.TryParse(Console.ReadLine().Trim().ToLower())) ? student.ID : Console.WriteLine("id isn't a number, try again");
What am I missing?

As I read it, you want to output something if the parsing fails. This can not be turned into a ternary.
There is a lot of things that are wrong with your code:
Tenaries are only for conditional assignments. You do not have a variable you assign anything to.
you never gave TryParse the out parameter, meaning the Compiler can not figure out wich function to even call there
Both options of a ternary must fit type you are doing assignment too. Console.WriteLine returns void, wich is propably not the same type you will have for Stundent.ID (I asume a integer).
So there are at least 3 reasons this can not compile.
Just keep it at the working if/else block. If you want to try ternaries, do stuff like applying a upper/lower bound for a value. That is what it is designed for. Rarely you use it for initialisation work (if == null, make a new instance. Otherwise give the value).
Edit: I just re-read and noticed that a assignment is not mandatory. It is just the very, very common usecase. And in that case, both results (or their return value) must fit the variable.

Christopher answered the question pretty well, so I won't repeat it here.
A workaround for what you're trying to do may be to implement a method that prompts the user for some input and doesn't return until the user enters a string that can be converted to the expected type (and optionally meets some other conditions).
For example:
private static int GetIntFromUser(string prompt, Func<int, bool> validator = null)
{
int result;
var cursorTop = Console.CursorTop;
do
{
ClearSpecificLineAndWrite(cursorTop, prompt);
} while (!int.TryParse(Console.ReadLine(), out result) ||
!(validator?.Invoke(result) ?? true));
return result;
}
private static void ClearSpecificLineAndWrite(int cursorTop, string message)
{
Console.SetCursorPosition(0, cursorTop);
Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, cursorTop);
Console.Write(message);
}
Now we can call this method in our main program and not worry about validation - our main code is much cleaner.
For example, say a student number must be 5 digits long, we could do something like:
int studentId = GetIntFromUser("Enter a 5-digit student id: ", x => x > 9999 && x < 100000);

Related

error: cs0131 The left-hand side of an assingnment must be a variable, property or indexer

I've tried to make a text adventure and a got an error when I get to the "if" part. I got it to work once but not like I wanted it.
I changed it a bit and then gave up and went to the original script but it wasn't working like last time and instead gave me this error:
cs0131 The left-hand side of an assingnment must be a variable,
property or indexer
Here is my code:
Console.WriteLine("What's your name");
string name = Console.ReadLine();
Console.Write("Hello " + name);
Console.WriteLine(" do you like games?");
Console.WriteLine("yes or no");
string yes = Console.ReadLine();
string no = Console.ReadLine();
if (Console.ReadKey() = yes) { Console.WriteLine("Great!, Lets play one"); }
//the error is at "if (console.readkey()"
In C# and many languages, there is a distinction between assigning a variable is equal to a value and testing for equality.
In C# = is used to assign values. int x = 1; will create a variable with the value 1.
== is used to test a value, so you would write if (x == 100) { /* something */ }
Going on your current code, you probably should have something more like this:
Console.WriteLine("yes or no");
string answer = Console.ReadLine();
if (answer == "yes") { Console.WriteLine("Great!, Lets play one"); }
Major differences are:
You are reading the answer a user types after asking yes or no, however in your code you are then trying to re-read another answer. Which doesn't quite make sense. The console will hang until the user enters another response.
As JamesFaix said, you are then trying to assign a value to Console.ReadKey() of whatever the user responded with first, after you asked them if they want to play. Instead you should be checking if the user's response was a positive reply.

How to add variable that changes depending on userinput to sortedlist C#?

The headline doesn't really fit but I'm not good at explaining in just one headline. Here goes: I want to put a double variable that changes depending on userinput into a Sortedlist and then loop through it so that it the output would be something like this:
Distance: 1
Distance : 2
Distance : 3
I can't use an arraylist because I don't know how many double values I will get.
I'm not really sure what you are asking for, so a wrote a code that will be "useful" for two scenarios: First, you want to access the last value that the user wrote. Second, you want to access all values on the list. And third, you want all of that on a single variable.
public class DistanceList : List<double>
{
public override string ToString()
{
int count = 1;
string resultString = String.Empty;
foreach (var item in this)
{
resultString = String.Concat(resultString, "Distance ", count.ToString(), ": ", item.ToString(), ";");
count = +1;
}
return resultString;
}
public static implicit operator double(DistanceList list)
{
return list.Single();
}
}
This class overrides the ToString method, and loops through all the values and add them on a string, giving the output you stated on your question "the output would be something like this: Distance: 1 Distance : 2 Distance : 3".
If you want the last value there is a implicit conversion for double which return the only value for the list, for one value will work just like a regular variable, for more than one value if you try to use as a double it will just trow an exception.
Just be aware that you will not be able to write directly to this variable and expect to add the item on the list, you will still need to use the List<T>().Add method.
But I really hope that it is not what you are asking for, because this kind of design will cause you a lot of trouble down the road.
Will you always be checking whether there is just one item so you can treat it as a double, or need to deal with many values? You will probably forget doing so somewhere...
This class only transforms them on a string, what if you need them as doubles to sum them up or something? You might end up parsing that string back into a List<double>...

Linq late binding confusion

Can someone please explain me what I am missing here. Based on my basic understanding linq result will be calculated when the result will be used and I can see that in following code.
static void Main(string[] args)
{
Action<IEnumerable<int>> print = (x) =>
{
foreach (int i in x)
{
Console.WriteLine(i);
}
};
int[] arr = { 1, 2, 3, 4, 5 };
int cutoff = 1;
IEnumerable<int> result = arr.Where(x => x < cutoff);
Console.WriteLine("First Print");
cutoff = 3;
print(result);
Console.WriteLine("Second Print");
cutoff = 4;
print(result);
Console.Read();
}
Output:
First Print
1
2
Second Print
1
2
3
Now I changed the
arr.Where(x => x < cutoff);
to
IEnumerable<int> result = arr.Take(cutoff);
and the output is as follow.
First Print
1
Second Print
1
Why with Take, it does not use the current value of the variable?
The behavior your seeing comes from the different way in which the arguments to the LINQ functions are evaluated. The Where method recieves a lambda which captures the value cutoff by reference. It is evaluated on demand and hence sees the value of cutoff at that time.
The Take method (and similar methods like Skip) take an int parameter and hence cutoff is passed by value. The value used is the value of cutoff at the moment the Take method is called, not when the query is evaluated
Note: The term late binding here is a bit incorrect. Late binding generally refers to the process where the members an expression binds to are determined at runtime vs. compile time. In C# you'd accomplish this with dynamic or reflection. The behavior of LINQ to evaluate it's parts on demand is known as delayed execution.
There's a few different things getting confused here.
Late-binding: This is where the meaning of code is determined after it was compiled. For example, x.DoStuff() is early-bound if the compiler checks that objects of x's type have a DoStuff() method (considering extension methods and default arguments too) and then produces the call to it in the code it outputs, or fails with a compiler error otherwise. It is late-bound if the search for the DoStuff() method is done at run-time and throws a run-time exception if there was no DoStuff() method. There are pros and cons to each, and C# is normally early-bound but has support for late-binding (most simply through dynamic but the more convoluted approaches involving reflection also count).
Delayed execution: Strictly speaking, all Linq methods immediately produce a result. However, that result is an object which stores a reference to an enumerable object (often the result of the previous Linq method) which it will process in an appropriate manner when it is itself enumerated. For example, we can write our own Take method as:
private static IEnumerable<T> TakeHelper<T>(IEnumerable<T> source, int number)
{
foreach(T item in source)
{
yield return item;
if(--number == 0)
yield break;
}
}
public static IEnumerable<T> Take<T>(this IEnumerable<T> source, int number)
{
if(source == null)
throw new ArgumentNullException();
if(number < 0)
throw new ArgumentOutOfRangeException();
if(number == 0)
return Enumerable.Empty<T>();
return TakeHelper(source, number);
}
Now, when we use it:
var taken4 = someEnumerable.Take(4);//taken4 has a value, so we've already done
//something. If it was going to throw
//an argument exception it would have done so
//by now.
var firstTaken = taken4.First();//only now does the object in taken4
//do the further processing that iterates
//through someEnumerable.
Captured variables: Normally when we make use of a variable, we make use of how its current state:
int i = 2;
string s = "abc";
Console.WriteLine(i);
Console.WriteLine(s);
i = 3;
s = "xyz";
It's pretty intuitive that this prints 2 and abc and not 3 and xyz. In anonymous functions and lambda expressions though, when we make use of a variable we are "capturing" it as a variable, and so we will end up using the value it has when the delegate is invoked:
int i = 2;
string s = "abc";
Action λ = () =>
{
Console.WriteLine(i);
Console.WriteLine(s);
};
i = 3;
s = "xyz";
λ();
Creating the λ doesn't use the values of i and s, but creates a set of instructions as to what to do with i and s when λ is invoked. Only when that happens are the values of i and s used.
Putting it all together: In none of your cases do you have any late-binding. That is irrelevant to your question.
In both you have delayed execution. Both the call to Take and the call to Where return enumerable objects which will act upon arr when they are enumerated.
In only one do you have a captured variable. The call to Take passes an integer directly to Take and Take makes use of that value. The call to Where passes a Func<int, bool> created from a lambda expression, and that lambda expression captures an int variable. Where knows nothing of this capture, but the Func does.
That's the reason the two behave so differently in how they treat cutoff.
Take doesn't take a lambda, but an integer, as such it can't change when you change the original variable.

When are curley braces required around single statements?

In my answer here: C# Lock syntax - 2 questions, LukeH pointed out that try...catch...(finally) statements require curly braces.
I found the answers as to why, found here ( Why do try..catch blocks require braces? ) very interesting.
I'd like to know of any more examples where curly braces are required as opposed to good practice etc, ideally with code snippet and explanation as to why.
Around a method body.
// not allowed:
int Inc(int x)
return x+1;
The why is not so easy, it would seem old-style C needed it more than C++/C#.
A little more about the why part, in (very) old C you would write
int Sum()
int a, b; // parameters, very informal
{
int s; // local var
...
}
So this ancient syntax needed the braces. And in all the languages that are based on C, nobody ever saw a point in making them optional, assuming that was possible in some cases.
Certain parts of language require braces to be present. For example, when you start a method you have to open and close braces to identify that as a code block. Inside a function certain language features like loops, conditinal statements, etc. also accept braces although in some cases they are not required. For example:
if (someValue == true)
doSomething();
In this case braces are not required, however you can surround this statement with braces, because you have just one statement that will be executed after if check, but if you want to execute multiple statement inside an if you need to use braces. For example,
if (someValue == true)
{
doSomething();
doSomeMoreWork();
}
Trying something like this is not allowed:
if (someValue == true)
doSomething();
doSomeMore();
else
doWork2();
int i = 1 + 2;
Compiler will complain in this case.
The problem can best be seen in the following loop:
while(i < 10)
doSomeWork();
i++;
Here you would expect i to increment, but this never happens. Basically this loop is the same as this one:
while(i < 10)
{
doSomeWork();
}
i++;
The statement inside the block will execute infinetly and i will never increment. In that case the proper way to write this statement would be:
while(i < 10)
{
doSomeWork();
i++;
}
Now you have a properly working statement. I like to use braces all the time regardless of number of statements that are being executed. The reason for this is that sooner or later I might need to add some more work in my if statement or inside a for or foreach loops. It's just a good practice.
You must use either braces or parentheses with checked and unchecked, depending on whether you're treating them as operators or statements:
// legal operator
int y = checked(x * 2);
// legal statement
unchecked
{
if ((a * b) > c)
{
DoSomething();
}
}
// illegal operator
int y = checked x * 2;
// illegal statement
unchecked
if ((a * b) > c)
DoSomething();
class/struct/interface declaration
class X { int _myval }

Do .. While loop in C#?

How do I write a Do .. While loop in C#?
(Edit: I am a VB.NET programmer trying to make the move to C#, so I do have experience with .NET / VB syntax. Thanks!)
The general form is:
do
{
// Body
} while (condition);
Where condition is some expression of type bool.
Personally I rarely write do/while loops - for, foreach and straight while loops are much more common in my experience. The latter is:
while (condition)
{
// body
}
The difference between while and do...while is that in the first case the body will never be executed if the condition is false to start with - whereas in the latter case it's always executed once before the condition is ever evaluated.
Since you mentioned you were coming from VB.NET, I would strongly suggest checking out this link to show the comparisons. You can also use this wensite to convert VB to C# and vice versa - so you can play with your existing VB code and see what it looks like in C#, including loops and anything else under the son..
To answer the loop question, you simple want to do something like:
while(condition)
{
DoSomething();
}
You can also do - while like this:
do
{
Something();
}
while(condition);
Here's another code translator I've used with success, and another great C#->VB comparison website. Good Luck!
//remember, do loop will always execute at least once, a while loop may not execute at all
//because the condition is at the top
do
{
//statements to be repeated
} while (condition);
Quite surprising that no one has mentioned yet the classical example for the do..while construct. Do..while is the way to go when you want to run some code, check or verify something (normally depending on what happened during the execution of that code), and if you don't like the result, start over again. This is exactly what you need when you want some user input that fits some constraints:
bool CheckInput(string input) { ... }
...
string input;
...
do {
input=Console.ReadLine();
} while(!CheckInput(input));
That's quite a generic form: when the condition is simple enough, it's common to place it directly on the loop construct (inside the brackets after the "while" keyword), rather than having a method to compute it.
The key concepts in this usage are that you have to request the user input at least once (in the best case, the user will get it right at the first try); and that the condition doesn't really make much sense until the body has executed at least once. Each of these are good hints that do..while is the tool for the job, both of them together are almost a guarantee.
Here's a simple example that will print some numbers:
int i = 0;
do {
Console.WriteLine(++i);
} while (i < 10);
using System;
class MainClass
{
public static void Main()
{
int i = 0;
do
{
Console.WriteLine("Number is {0}", i);
i++;
} while (i < 100);
}
}
Another method would be
using System;
class MainClass
{
public static void Main()
{
int i = 0;
while(i <100)
{
Console.WriteLine("Number is {0}", i);
i++;
}
}
}
The answer by Jon Skeet is correct and great, though I would like to give an example for those unfamiliar with while and do-while in c#:
int i=0;
while(i<10)
{
Console.WriteLine("Number is {0}", i);
i++;
}
and:
int i=0;
do
{
Console.WriteLine("Number is {0}", i);
i++;
}while(i<10)
will both output:
Number is 0
Number is 1
Number is 2
Number is 3
Number is 4
Number is 5
Number is 6
Number is 7
Number is 8
Number is 9
as we would expect. However it is important to understand that the do-while loop always executes the body the first time regardless of the check. This means that if we change i's starting value to 100 we will see very different outputs.
int i=100;
while(i<10)
{
Console.WriteLine("Number is {0}", i);
i++;
}
and:
int i=100;
do
{
Console.WriteLine("Number is {0}", i);
i++;
}while(i<10)
Now the while loop actually generates no output:
however the do-while loop generates this:
Number is 100
despite being well over 10. This is because of the unique behavior of a do-while loop to always run once unlike a regular while loop.
Apart from the Anthony Pegram's answer, you can use also the while loop, which checks the condition BEFORE getting into the loop
while (someCriteria)
{
if (someCondition)
{
someCriteria = false;
// or you can use break;
}
if (ignoreJustThisIteration)
{
continue;
}
}

Categories

Resources