Is there a public: equivalent in C#

  • Thread starter Thread starter Brian
  • Start date Start date
B

Brian

This has most likely been asked before, but Google still won't allow
certain search strings such as "public:".

In CPP, you can declare a class such as...

class Foo
{
public:
Foo()
{
}

~Foo()
{
}

int m_Bar1;
int m_Bar2;
int m_Bar3;
};

If an object is based on Foo, then all of the class' members will be
accessible to it.

whereas in C# it would seem to be...

class Foo
{
public Foo()
{
}

~Foo()
{
}

public int m_Bar1;
public int m_Bar2;
public int m_Bar3;
}

Can I define "blocks" of public variables in C# or must I practice my
typing on the word "public" for *every* public member and member function?

Thanks,
Brian
 
You can't do that in .NET.
Need to explicitly define all member access.
If you don't declare a member access modifier, you'll get the default which
is *private*

Cheers,
Branimir
 
This has most likely been asked before, but Google still won't allow
certain search strings such as "public:".

In CPP, you can declare a class such as...

class Foo
{
public:
Foo()
{
}

~Foo()
{
}

int m_Bar1;
int m_Bar2;
int m_Bar3;
};

If an object is based on Foo, then all of the class' members will be
accessible to it.

whereas in C# it would seem to be...

class Foo
{
public Foo()
{
}

~Foo()
{
}

public int m_Bar1;
public int m_Bar2;
public int m_Bar3;
}

Can I define "blocks" of public variables in C# or must I practice my
typing on the word "public" for *every* public member and member function?
As already stated, C# doesn't support this, nor should it really. In C++
members were defined in a fairly compact header file. The proximity of
public: to the declaration was likely quite near. In c#, however, the
distance between the two may be many hundreds of lines. Placing public on
the member makes it *alot* easier to determine the accessibility of the
member while viewing that member instead of having to scan up looking for
the most recent <accessibilityModifier>:.

Public blocks for parameters could be nice, but I think it'd need a clearer
syntax than public:. However that is pretty irrelevent to your question, so
I'll wander off now.
 
<Brian> wrote:

Can I define "blocks" of public variables in C# or must I practice my
typing on the word "public" for *every* public member and member function?

Yes, you need to mark each method with its accessibility separately.
However, you only need to type out the method name once, rather than
also having it in a header file :)

Personally I prefer the C# way:

1) It's easier to see the accessibility of a method without looking up
potentially pages and pages of code.

2) You can move methods around without fear that their accessibility
will change.

3) You can group methods by logical use rather than by accessibility.
(For instance, I often have a public method which calls various private
methods, and have the public method directly above the private methods.
I can then slap a region round the lot with an appropriate comment.)
 
Although it's a little too wordy for my taste, I can see advantages to
putting the public, private, etc. keyword in front of all members. I'm
a creature of habit, and I'll probably never be completely convinced,
however. :) I won't be giving up on the language simply because of that.
That would be rather shallow.

Thanks to all who answered.

Brian

This has most likely been asked before, but Google still won't allow
certain search strings such as "public:".
In CPP, you can declare a class such as...
class Foo
{
public:
Foo()
{
}
~Foo()
{
}

int m_Bar1;
int m_Bar2;
int m_Bar3;
};
If an object is based on Foo, then all of the class' members will be
accessible to it.
whereas in C# it would seem to be...
class Foo
{
public Foo()
{
}

public int m_Bar1;
public int m_Bar2;
public int m_Bar3;
}
 
Back
Top