473,387 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Is there a public: equivalent in C#

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
Nov 16 '05 #1
4 1794
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

--
Branimir Giurov
MCSD.NET, MCDBA
eAgility LLC

<Brian> wrote in message news:10*************@corp.supernews.com...
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

Nov 16 '05 #2

<Brian> wrote in message news:10*************@corp.supernews.com...
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.
Thanks,
Brian

Nov 16 '05 #3
<Brian> wrote:

<snip>
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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
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
I wrote:
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


--
Brian
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Petra Neumann | last post by:
Hello, I just came across some sample code where a variable has been defined as: static public final int TABLE = 1, LIST = 2; I was wondering if there is a difference to writing public static...
175
by: Ken Brady | last post by:
I'm on a team building some class libraries to be used by many other projects. Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be...
5
by: Urs | last post by:
Hi all, I'm rather new to C# so please forgive my ignorance. I'd like to list the public, non-inherited methods of a class. I thought MethodInfo mi = typeof(string).GetMethods(...
3
by: Brian | last post by:
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() {
12
by: Andrew Schepler | last post by:
When compiled with Visual C++ .NET 2003 (only), the program below aborts as though no matching catch clause is present. If the copy constructor of A is made public, it successfully catches the...
5
by: DMitchell | last post by:
Is there any way of getting a public enumeration to be generated into a web service proxy?
3
by: Joel Moore | last post by:
Is there any point to making classes or interfaces public in an EXE project? Executables can't be referenced by other applications so why would you ever need public classes? Do people leave...
26
by: Zytan | last post by:
What happens if I do this: static byte MemberFunction() instead of: public static byte MemberFunction() I know I can't access it. But what does it default to? Private? I can't find any code...
15
by: jmDesktop | last post by:
Consider: <?php echo "start"; class myClass { public $connection;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.