-2

Maybe this is silly, but I'm trying to shorten the calling of the method StreamWriter.WriteLine becasue I have to call it many times throughout my code. So, instead of calling myFile.WriteLine() I would like to write just myFile.WL().

Is this possible?

After searching for a solution I wrote this:

private static void WL(this StreamWriter myFile, params string myString)
{
    return (void)myFile.WriteLine(myString);
}

but I get the error

Cannot convert type 'void' to 'void'

Any ideas?

Leonardo Trivino
  • 295
  • 1
  • 4
  • 11
  • 1
    Try taking out the `return` so looks like just `myFile.WriteLine(myString);` - since `void` just means it doesn't return anything. – ahwm Jun 03 '15 at 18:40
  • 4
    `myFile.WriteLine()` to `myFile.WL` ??? What, really ?? how much this would help ? and have you thought about readability ? – Habib Jun 03 '15 at 18:40
  • https://msdn.microsoft.com/en-us/library/ms173114.aspx – Jeroen Vannevel Jun 03 '15 at 18:41
  • OK, OK, I guess the question was silly after all, no need to panic! Still it's useful to know what was wrong with my attempt. Thanks for the answers anyway. – Leonardo Trivino Jun 03 '15 at 18:46
  • 1
    Please don't do that. Anyone else (including yourself in a few years time) reading your code will likely know what `WriteLine` does and will have no idea what `WL` is. You are making the reader's life difficult just to save yourself a few seconds of effort. The golden rule of good code is "Always prioritise readability over everything". Please don't break this rule. – David Arno Jun 03 '15 at 18:56

2 Answers2

4

There is no need for the extension method, but just for the sake of completeness...

Remove the return statement from your method, as it doesn't have to return anything.

private static void WL(this StreamWriter myFile, params string myString)
{
    myFile.WriteLine(myString);
}

BUT, Reconsider what you are trying to do. There is no point in shortening WriteLine to WL, it doesn't serve any purpose, it will make the code less readable, also you will not be able to cover up all the overloads.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • 1
    OK, OK, I guess the question was silly after all, no need to panic! Still it's useful to know what was wrong with my attempt. Thanks for the answers anyway – Leonardo Trivino Jun 03 '15 at 18:48
  • @LeonardoTrivino, you are welcome, one more thing to add, if you are writing extension methods then in most of the cases they should be public, as they will *probably* be used outside the class. – Habib Jun 03 '15 at 18:58
0

Your Extension method should only be

private static void WL(this StreamWriter myFile, params string myString)
{
    myFile.WriteLine(myString);
}

Reasons:

  • WriteLinemethod of StreamWriter does not return anything i.e. void. It only and I quote Writes a string followed by a line terminator to the text string or stream. So you should remove return keyword from the method. Read here.

BTW, Extension method should probably be public if you want to use it outside of the class you define it in.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
  • 1
    This is absolutely the answer to the question. What the OP is doing may be a bad idea, but that doesn't mean a correct answer should be down voted. – juharr Jun 03 '15 at 18:47
  • OK, OK, I guess the question was silly after all, no need to panic! Still it's useful to know what was wrong with my attempt. Thanks for the answers anyway – Leonardo Trivino Jun 03 '15 at 18:49
  • 1
    Well now there is a problem. Extensions methods can be private. – juharr Jun 03 '15 at 18:49
  • 1
    @juharr: Yes. But it is advisable to keep them public. It seems OP is a learner and thought to let him stick to basics. – Nikhil Agrawal Jun 03 '15 at 18:52
  • @NikhilAgrawal Then you should say that the extension method should probably be public if you want to use it outside of the class you define it in. It's best to be explicit. – juharr Jun 03 '15 at 18:53