The below may not look like much, but the ability to call methods in other classes, offloading all the work onto other classes from MainClass, and returning a variable to be passed to the next method is a rather large leap for me at least.
using System;
namespace CSharpTutoriel
{
class Work
{
public static string TellMeWhatToSay(out string passTheQuote)
{
string whatIsWritten;
Console.WriteLine("Tell Me What To Say");
whatIsWritten = Console.ReadLine();
passTheQuote = whatIsWritten;
return passTheQuote;
}
public static void HelloWorld(string itWillBeSpoken)
{
Console.WriteLine(itWillBeSpoken);
}
public static void PressToContinue()
{
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
}
class MainClass
{
public static void Main(string[] args)
{
string whatToSay;
Work.TellMeWhatToSay(out whatToSay);
Work.HelloWorld(whatToSay);
Work.PressToContinue();
}
}
}
-
\$\begingroup\$ CSharpTutoriel has a typo in it, unless you speak French. \$\endgroup\$dfhwze– dfhwze2019年07月22日 16:40:18 +00:00Commented Jul 22, 2019 at 16:40
2 Answers 2
Redundancy of both out
parameter and return
public static string TellMeWhatToSay(out string passTheQuote)
{
string whatIsWritten;
Console.WriteLine("Tell Me What To Say");
whatIsWritten = Console.ReadLine();
passTheQuote = whatIsWritten;
return passTheQuote;
}
You both write the result to an out
parameter and return it. A violation the DRY principle, this outputs the same information twice. As the out
parameter does not feel appropriate for a modern language to me, I will leave only the return
:
public static string TellMeWhatToSay()
{
string whatIsWritten;
Console.WriteLine("Tell Me What To Say");
whatIsWritten = Console.ReadLine();
passTheQuote = whatIsWritten;
return passTheQuote;
}
Now that we eliminated that redundancy, we can simplify by returning directly:
public static string TellMeWhatToSay()
{
Console.WriteLine("Tell Me What To Say");
return Console.ReadLine();
}
Unecessary alias of a built-in Console.ReadLine
public static void HelloWorld(string itWillBeSpoken)
{
Console.WriteLine(itWillBeSpoken);
}
HelloWorld
is the same as Console.WriteLine
so there is no need to write it.
-
\$\begingroup\$ While yes, 'HelloWorld' is completely unnecessary, the point of the exercise was to both offload all work out of MainClass and to pass a variable from one method to another. This is at least the only way I know how to do that. \$\endgroup\$Clol7– Clol72016年10月26日 21:20:06 +00:00Commented Oct 26, 2016 at 21:20
-
\$\begingroup\$ @Clol7
var x = FirstFunction(); var y = SecondFunction(x)
use return values to pass "variables" between functions (really values) \$\endgroup\$Caridorc– Caridorc2016年10月26日 21:22:41 +00:00Commented Oct 26, 2016 at 21:22 -
\$\begingroup\$ Once you removed the
out string passTheQuote
declaration, the assignmentpassTheQuote = whatIsWritten
became invalid. Remove it, too; and replacepassTheQuote
in thereturn
statement withwhatIsWritten
. \$\endgroup\$CiaPan– CiaPan2016年10月27日 08:05:24 +00:00Commented Oct 27, 2016 at 8:05 -
\$\begingroup\$ @CiaPan i covered that in the third code sample \$\endgroup\$Caridorc– Caridorc2016年10月27日 14:15:06 +00:00Commented Oct 27, 2016 at 14:15
-
1\$\begingroup\$ Yes, but the second one remains invalid. It may be worth noting that it is intentionally presented in an intermediate state. \$\endgroup\$CiaPan– CiaPan2016年10月27日 19:38:24 +00:00Commented Oct 27, 2016 at 19:38
You do not need the out
attribute here:
public static string TellMeWhatToSay(out string passTheQuote)
{
//...
return passTheQuote;
}
What out
does, is it requires the parameter to be assigned somewhere in the method a value is returned. It is only to be used in places such as:
public bool trySomething(out parameter)
{
parameter = someValue;
return somethingSucceeded;
}
Notice how we are supplying two values to the caller in the above method--the return value and the out
parameter value.
If you don't use the out
flag, you can change this:
public static string TellMeWhatToSay(out string passTheQuote) { string whatIsWritten; Console.WriteLine("Tell Me What To Say"); whatIsWritten = Console.ReadLine(); passTheQuote = whatIsWritten; return passTheQuote; }
to:
public static string TellMeWhatToSay()
{
Console.WriteLine("Tell Me What To Say");
string whatIsWritten = Console.ReadLine();
return whatIsWritten;
}
Given what you are using this for, the use of static
members of the Work
class is alright, although I'd also make the class static. However, for the most part, you should not work with static
members like this--you should learn how to instantiate a class and use specific instances.
You should allow the user to pass a custom prompt to TellMeWhatToSay
, instead of just having one prompt. This will make your input method much more flexible.
-
\$\begingroup\$ From what I understand of
static
is that it prevents a method from being discarded after use, it didn't seem like a good idea to omit it. But I'll take a look at that. \$\endgroup\$Clol7– Clol72016年10月26日 21:29:15 +00:00Commented Oct 26, 2016 at 21:29 -
2\$\begingroup\$ No, an instance of a class always has the methods available. Methods that are static are available outside of instances because they don't carry state. \$\endgroup\$user34073– user340732016年10月27日 04:19:49 +00:00Commented Oct 27, 2016 at 4:19
-
1\$\begingroup\$ @Clol7
static
prevents a method from being discarded after use :-| where did you overhear this? What should it mean at all? That the method cannot be used anymore? \$\endgroup\$t3chb0t– t3chb0t2016年10月27日 12:47:11 +00:00Commented Oct 27, 2016 at 12:47 -
\$\begingroup\$ @t3chb0t I'm not sure where I heard about it, actually. But what I meant by that was the current state of the method.... now that I think about it that doesn't make sense either. \$\endgroup\$Clol7– Clol72016年10月27日 22:53:16 +00:00Commented Oct 27, 2016 at 22:53