Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having a "+" in the class name?

Class name: MyAssembly.MyClass+MyOtherClass

The problem is obviously the + as separator, instead of traditionnal dot, its function, and to find official documentation to see if others separators exist.

like image 855
Graveen Avatar asked Mar 14 '10 18:03

Graveen


People also ask

Can you have spaces in class names?

The class name can't contain a space, but it can contain hyphens or underscores. Any tag can have multiple space-separated class names.

Can Java class names have spaces?

A class name is an identifier—a series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ( $ ) that does not begin with a digit and does not contain spaces.

Can we define a method with the same class name in C#?

Yes, It is allowed to define a method with the same name as that of a class.

Can there be space in class in HTML?

A class name can't have spaces. When you have a space-separated string in your class attribute, you're always giving your element several classes.


2 Answers

That's just the way that a nested type is represented. So for example:

namespace Foo
{
    class Outer
    {
        class Nested {}
    }
}

will create a type with a full name of Foo.Outer+Nested in the compiled code. (So that's what typeof(Outer.Nested).FullName would return, for example.)

It's not clear to me whether this is specified behaviour, or just what the Microsoft C# compiler chooses to use; it's an "unspeakable" name in that you couldn't explicitly declare a class with a + in it in normal C#, so the compiler knows it won't clash with anything else. Section 10.3.8 of the C# 3 spec doesn't dictate the compiled name as far as I can see.

EDIT: I've just seen that Type.AssemblyQualifiedName specifies that "+" is used to precede a nested type name... but it's still not clear whether or not that's actually required or just conventional.

like image 75
Jon Skeet Avatar answered Oct 03 '22 04:10

Jon Skeet


This is what the compiler uses in the metadata to represent a nested class.

i.e.

class A { class B {} }

would be seen as

class A+B

in the metadata

like image 43
Ben Voigt Avatar answered Oct 03 '22 06:10

Ben Voigt