1

I am new to programming here. This is a question that i have been mulling over. Can you set a variable to Console.ReadLine() in c# and then call the variable instead of typing Console.ReadLine() everytime? For example:

//Set Variable
var read = Console.ReadLine();
//Call vaariable
read;
soso
  • 311
  • 1
  • 4
  • 13
  • what do you mean by calling variable ? you *use* a variable like `Console.WriteLine(read);` – Habib Dec 04 '15 at 18:28

2 Answers2

4

Like this:

//Set Variable
Func<string> read = Console.ReadLine;
//Call vaariable
read();
mshsayem
  • 17,557
  • 11
  • 61
  • 69
1

It sounds like you want to create a delegate, like this:

var readOp = new Func<string>(() => Console.ReadLine());

and then you can use it like this:

System.Diagnostics.Debug.Print(readOp());

or this, or whatever else:

string myLine = readOp();
rory.ap
  • 34,009
  • 10
  • 83
  • 174