简体   繁体   中英

(Trivial?) C# namespace/class hierarchy problem

I am somewhat new to C# and I am puzzled by a compilation result belonging to a VERY small, simple and transparent program I have written and which is part of a university project. The code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hemtenta_problem_1
{
    public class Basklass
    { 
        public virtual void SkrivTrams()
        {
            Console.WriteLine("Hej Hopp");
        }
    }
    public class Avledd_klass : Basklass
    {   
                    public override void SkrivTrams()
        {
            Console.WriteLine("Hej Hå");
        }
    }
    public class Avledd_klass_till : Avledd_klass
    {
        public new virtual void SkrivTrams()
        {
            Console.WriteLine("Tjo Hej");
        }
    }
    public class Ytterligare_avledd_klass : Avledd_klass
    {

    }_ //row which gives compiler message

    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

The compiler's message, relating to the row with the comment, is

A namespace cannot directly contain members such as fields or methods

Now, I perfectly well understand that any field or method can't be placed out there "in the open". It needs to be part of a class, and inside one. BUT IT IS NOT A FIELD OR A METHOD THAT I AM CODING, IT IS A CLASS IN ITSELF. None of the other classes, above in the code, gave this problem. It started when I wrote the very last and as yet empty class.

Is Visual Studio's compiler working right? Or what is it in 40-odd lines that I am not seeing? Very grateful for comments that could help.

You have an _ after your closing } on the line where you have put your error code. You need to remove it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM