Why Modifier can't be set like C++

I am using C++ Language and learning in C#.

But I used to set Public modifier as

class cls
{
-----param

public :

-----method and param
}

Why C# don't have this. I think all modifier should be able to set as this method like C++. And can set like Java too.

Or Microsoft has some reason



Answer this question

Why Modifier can't be set like C++

  • progames25

    Why it can't is syntax. Why it was designed that way has already been pointed out: to clarify member modifiers and to increase quality (by avoiding accidental and un-obvious modifier changes because a declaration's class offset was changed resulting in a different modifier).

  • kcchesnut

    SmallTalk and C++ Access Modifier was able to applied to more than one member by one cast.

    with "public :" or "private :" or "protected :" in 1 row

     

    Why C# can't


  • shevi2027

    sorry, can you explain a bit more in depth



  • Cest la vie

    You should not declare your variables public. Good programming practice is to create a property for private variable that had to be exposed.

  • den2005

    Yes,so I want to know why it can't

    Why microsoft can't make VC# to set any modifier in 2 ways

    like Java as it is.
    And like SmallTalk as VC++ can be.

    Microsoft have any reason to remove this Syntax, Or it have any problem

    Or they forgot to do


  • ComputerSue

    It's because the separate access modifier syntax is prone to getting disconnected from what it applies to. For example, with larger class definitions:

    class SomeClass {
    int value1;
    int value2;

    public:
    int value3;
    int value4;
    int value5;
    int value6;
    int value7;
    int value8;
    int value9;
    int value10;
    int value11;
    int value12;
    int value13;
    int value14;
    int value15;
    int value16;
    int value17;
    int value18;
    int value19;
    int value20;
    int value21;
    int value22;
    int value23;
    int value24;
    int value25;
    int value26;
    int value27;
    int value28;
    int value29;
    int value30;
    int value31;
    int value32;
    //...

    };

    it's easy to scroll down to the bottom of the class and not know what the access is for a value because the access specifier applies to it could be several dozen lines above it. Cutting and pasting lines of code exacerbates this because the location of a line of code essentially defines it's access level. Because of this access specifiers must be applied directly to each member declaration (if the default is not sufficient).



  • colindyck

    Moving this thread to the C# Language Forum as this is a question about the language, not the IDE.

    -Tom Meschter
    Software Dev, Visual C# IDE



  • Michael Herman - Parallelspace

    Yes this is true and its not possible in C#.

    Best Regards,

    Rizwan



  • Sabbadin

    I'm not clear on what your question is. All modifiers (static, const, private, public, internal, protected, etc.) must be applied per member. I don't know what you mean by "if Microsoft try to do"; try to do what

  • hrubesh

    Yes,Right. Thank you Mr.Peter.

    And so, we can use private and protect with this technic.
    And if Microsoft try to do. We may be able to set static,const, or any type modifier

    like this

    class class1
    {
    //private :
    int x,y,z;

    static :
    const int A = 20;
    protected :
    const double B = 5;
    int C;

    public :
    int M(){};
    int N(){};

    private int O()
    {M() * N()};

    int Z()
    {O()};
    }

    if this have more than 20 method. it may so tired.
    Set modifier like SmallTalk have more Advantages

    So why Microsoft remove this ability I want to know


  • Why Modifier can't be set like C++