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:

// 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

  • First, you need to get rid of the namespace declaration (and its braces) - LINQPad adds that for you automatically.

    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'):
    static void Main()
    {
        Console.WriteLine (FeetToInches (30));      // 360
        Console.WriteLine (FeetToInches (100));     // 1200
        Console.WriteLine (FeetToInches (0));
    }
    
    static int FeetToInches (int feet)
    {
        int inches = feet * 12;
        return inches;
    }
    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.
  • Thanks for the quick reply Joe. I was working from the downloaded C# 6 samples and just added some components I'd normally use in VS2017. I downloaded the C# 7 samples but haven't noticed much difference yet. I think you answered my question though concerning the namespace. namespace cannot be used in any queries.

    Kev
  • That's exactly the information I was looking for. Now things are snapping into place. I see how UserQuery fits in and how "class EOF {" actually helps makes things look as I expected them to look.

    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
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!