namespace cs1513 } expected
Been studying c# as a hobbyist for a while now. Just picked up LINQPad and decided to go back to the beginning and maybe try different things. That seems to be the beauty of LINQPad for me. Easy and quick playground. The side effect is that when something doesn't work as I expected I'm having a hard time knowing if it is my understanding of c# (high probability) or something about LINQpad I don't understand. At any rate, here's my question.
When working through some examples I decided to add "missing" class and namespace to the code. When I encompass the code with namespace TestPrograms {} I get the error cs1513 } expected. All my braces are matched. Hovering over the last brace shows (namespace) and there's the red squiggly right after that brace. This works fine in VS2017.
Here's the code I have:
When working through some examples I decided to add "missing" class and namespace to the code. When I encompass the code with namespace TestPrograms {} I get the error cs1513 } expected. All my braces are matched. Hovering over the last brace shows (namespace) and there's the red squiggly right after that brace. This works fine in VS2017.
Here's the code I have:
// Here, we've refactored the logic in our original main method into a method called FeetToInches.
// (We have to go back to 'Program' mode for this.)
namespace TestPrograms
{
class Test
{
static void Main()
{
// This works when not static and placed within Main(); Why can't it be called outside of Main()? I have a feeling
// I'm missing something basic.
// int FeetToInches(int feet)
// {
// int inches = feet * 12;
// return inches;
// }
Console.WriteLine(FeetToInches(30)); // 360
Console.WriteLine(FeetToInches(100)); // 1200
Console.WriteLine(FeetToInches(0));
}
// what is the purpose of setting FeetToInches() to static?
//static int FeetToInches (int feet)
//{
// int inches = feet * 12;
// return inches;
//}
static int FeetToInches(int feet)
{
int inches = feet * 12;
return inches;
}
// I fell into a pattern of just declaring everything static because "that's the only way it worked"
}
}
Comments
You can also (optionally) get rid of the class declaration. In LINQPad, it's perfectly valid to have just methods (when you set the language to 'C# Program'): You only need to think about writing a class in more complex cases (e.g., when you want multiple classes).
I notice that your example code is from C# in a Nutshell. Can I suggest that you go to the samples tab in bottom left and click 'Download/import more samples'. Choose the sample library "C# 7 in a Nutshell". It will then download all the samples from the early chapters as valid LINQPad queries. In particular, the first samples (Chapter 2, language basics) walk you through how LINQPad scripts differ from code you would write in Visual Studio.
Kev
So, let me take this chance to say thanks to LINQPad and it's developers. I've only been using it for a bit less than a week now but it has totally helped me to go back and relearning c# with a lot more understanding than my previous approach.
Kev