How to define data types directly in a namespace

M3rein

What I'm trying to find out whether you can have something like underneath or not:

namespace myNamespace
{
    public string myString;
}

I couldn't find anything that explained this, probably because of my terrible searching keywords. Could anyone tell me if there is a way to do this or not?

If this is possible, I may have goofed up something. Either way, I'd like to know.

I wanted to know how to do this because I want to have access to this in the whole namespace without having to go something.myString when I'm inside a different class. This was something that seemed logical in my eyes, but it's apparently not possible.

Dmitry Bychenko

You can't declare field directly in the namespace:

    // doesn't compile
    namespace myNamespace
    {
        public string myString; // <- syntax error
    }

but you can emulate such a syntax with a help of using static (C# 6.0+):

    namespace MyLibrary 
    {
        // put myString within a static class
        public static class MyStorage 
        {
            // let turn field into property
            public static string myString {get; set;}
        }
    }

Then use the static class with using static:

    // please, notice "using static"
    using static MyLibrary.MyStorage;

    namespace myNamespace
    {
        public class MyClass 
        {
            public void MyMethod() 
            {
                myString = "abc"; // as if it has been declared in the namespace

                string test = myString;  
                ...
            }
        } 

    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to define namespace in python?

How to define functions and data from another namespace, not in the global one?

How are C data types “supported directly by most computers”?

How do I define a data type consisting of other data types?

How is #define S64_MIN defined in linux data types?

Mongoose Data Types how to define multiple type values ? ESLint error

How to define a function returning one of multiple data types?

How to import lodash directly into a custom namespace in JavaScript

How to define nullable types if nested types are not supported

How to define namespace name same as return type of function in that namespace?

How do I define a variable in a namespace with "using namespace"?

How to define mutually recursive types

How to define abstract types in agda

how to define types as interfaces in Typescript

How to define a friend operator declared on a template in a namespace?

How to define a reusable function in vue component namespace

How to define/declare xaml namespace in WPF?

Define empty data.table directly with the correct data type

type parameter in traits to define data types in scala

Is it possible to define variadic-kinded data types?

Define a enum data types for use in several files

How to use Vuex types constants with module namespace?

How can you define the uint_fast32_t for MPI data types?

How to define data type?

How to load xml data with namespace

How to define a implicit (given) Conversion for Generic Types?

How to properly define types for a `withProps` function for React?

How to define a function parameter to be either of two types

How to define impl on standard types in Rust?