Sign Up

Continue with Facebook
Continue with Google
or use


Have an account? Sign In Now

Sign In

Continue with Facebook
Continue with Google
or use

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

WhatisWhatis

WhatisWhatis Logo WhatisWhatis Logo

WhatisWhatis Navigation

  • Blog
  • Write For Us
  • About Us
Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Home
  • All Questions
  • Polls
  • Followers
  • FAQs
  • Add A Group
  • Add A Post
  • Blog
  • Write For Us
  • About Us
Home/namespace error

WhatisWhatis Latest Questions

Prabhakar Atla
  • 1
Prabhakar Atla
Asked: January 29, 2022In: Technology, Website Development

a namespace cannot directly contain members such as fields or methods

  • 1

Define Compiler Error Code CS0116. What does a namespace which cannot directly contain members such as fields or methods in c#? How can you fix it?

a namespace cannot directly contain members such as fields or methodsC#CS0116ErrorNamespacenamespace error
  1. Best Answer
    Prabhakar Atla
    Added an answer on January 29, 2022 at 2:04 pm

    A namespace cannot directly contain members such as fields or methods: If you are new to coding, you may definitely come across a compiler error. If it shows the error code—CS0116, you have to get it fixed. Usually, such errors occur in C#. You will usually notice the error if a namespace cannot dirRead more

    A namespace cannot directly contain members such as fields or methods:

    If you are new to coding, you may definitely come across a compiler error. If it shows the error code—CS0116, you have to get it fixed. Usually, such errors occur in C#. You will usually notice the error if a namespace cannot directly contain members such as fields or methods.

    What Does This Error Signify?

    If you have recently started working on coding or C#, noticing such errors is common. You need to brush up coding concepts and just go through some introductory chapters to get familiar with namespace. 

    What does a namespace error or compiler error code-CS0116 look like?

    namespace DeveloperPublishNamespace

    {

        public int Field1;

        public class DeveloperPublish

        {

            public static void Main()

            {

     

            }

        }

    }

    Error: A namespace cannot directly contain members such as fields or methods

    Solution: Move the fields or methods into a class from namespace.

    Correct use of Namespace – An Example:

    namespace MyNameSpace

    {

       int i; <– THIS NEEDS TO BE INSIDE THE CLASS

     

       class MyClass

       {

          …

       }

    }

    Note: If you can’t recognize what’s “outside” the class, consider the addition/ deletion of misplaced or extra closing bracket(s) }.

    100% Correct Solution—Points to Remember:

    • Remove one of the closing curly braces from the three in a row.
    • Place a curly brace after the Swing Axe function. 
    • The use of a third closing curly brace starts post update function. 
    • SwingAxe Function is currently outside the class.
    • You have used invalid syntax and coding structure.

    Note: Next time, don’t declare a method or field under namespace. They should always be mentioned in the class.

    In Conclusion:

    Thus, namespace error can be easily detected and rectified using the correct way of placing brackets and curly braces. It’s one of the commonest errors that every user when they start working on C#. So, next time you notice a namespace cannot directly contain members such as fields or methods, you need not worry. Just make sure that your syntax is correct and you can easily get rid of the error.

    See less
      • 1
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
  • 1 Answer
  • 223 Views

Sidebar

Ask A Question
Add A Post
Create A New Group

Categories

Our Figures

  • Questions 1k
  • Answers 1k
  • Posts 1k
  • Comments 92
  • Best Answers 1k
  • Users 13k

Explore

  • Home
  • All Questions
  • Polls
  • Followers
  • FAQs
  • Add A Group
  • Add A Post

Footer

About Us

WhatisWhatis.com is trying to become one of the best question and answer platforms out there, which will help you establish your community and connect with other people.

Our Pages

  • About Us
  • Blog
  • Contact Us
  • Frequently Asked Questions (FAQs)
  • Homepage
  • Privacy Policy
  • Write For Us To Submit A Guest Post Article on Technology, Education, Health, Apps, Gadgets, IoT, AI, Business, Digital Marketing and More!

Subscribe For Us

DMCA.com Protection Status

Our Feeds

Sitemap

RSS Feeds

Follow Us Here

[njwa_button id="80129"]

© 2019 - 2022 All rights reserved By WhatisWhatis.com