Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A namespace cannot directly contain members? [duplicate]

Tags:

c#

I have a problem. I have been following a tutorial, so that I could learn programming with Xamarin. Well now I have this error line, that I have written in the title. Here's the code for you all;

using System.Collections.ObjectModel;
using UIKit;

namespace freesongsforme
{
    static void Main(string[] args)
    { 
        var listView = new ListView();
        var myTrainers = new ObservableCollection<string>() 
        { 
            "Song 1",
            "Song 2",       
        };

        listView.ItemsSource = mySongs; 

        myTrainers.Add("Songsr");
    }

The ''main'' part is marked as an error.

like image 638
evanthomas570 Avatar asked Jun 02 '16 12:06

evanthomas570


People also ask

What do you mean by namespace?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

What is a namespace in C#?

The namespace keyword is used to declare a scope that contains a set of related objects. You can use a namespace to organize code elements and to create globally unique types.

What does error CS0116 mean?

Error CS0116: A namespace cannot directly contain members such as fields or methods - Unity Forum.


1 Answers

your code needs to be contained within a Class

namespace freesongsforme
{

  public class MyClass {

    static void Main(string[] args)
    { 
      var listView = new ListView();
      var myTrainers = new ObservableCollection<string>() { 

        "Song 1",
        "Song 2",

      };

    listView.ItemsSource = mySongs; 

    myTrainers.Add("Songsr");

  }
}
like image 152
Jason Avatar answered Oct 18 '22 16:10

Jason