开发者

Basic class naming and visibility questions

开发者 https://www.devze.com 2022-12-21 00:09 出处:网络
I always see people giving me code (I mean code examples for questions on SO and the like) like: class MyData{

I always see people giving me code (I mean code examples for questions on SO and the like) like:

    class MyData{
       Observ开发者_运维问答ableCollection<Color> Colors;
       ObservableCollection<Fruit> Fruits;
       ObservableCollection<Pairs> Pairs;

       public void MatchCurrentSelection(){
            .....
etc
            }
       }
    } 

Everytime I start writing code in visual studio, it forces me to declare the visibility, and always included in a namespace. Do people jsut leave out the namespace as its irrelevant? And why am I always forced to set the visibilty? (It appears to automatically append private by default)

    namespace TheProject
    {
        public class MyData
        {
            private ObservableCollection<Colors> Colors;
            private ObservableCollection<Fruits> Fruits;
...

    etc

I have to be missing something here...What's the story?

Thanks


If by "giving me code", you mean providing samples/examples on stackoverflow, then visibility and namespacing has probably been ommited for brevity, I know I do!

It's the same reason a large amount of sample code (here, and on places like msdn.microsoft.com) ommit most of the error-checking/handling, because having all that makes it harder to see the intent of the specific bit of code being provided as an example.


You should normally get the namespace that is the default of your project when you add a class to your project in VS 2008 SP1. This is what I got 2 minutes ago.

As far as the class is concerned, the default template has no visibility attribute, so it is internal (not private).

I guess the idea is that making it public has consequences, so you should do it explicitly.

Personally, I prefer my classes to be public by default, so I edited the VS template at: c:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

Inside, the class.cs file is the one to edit. VS has to be restarted for the change to take effect.

0

精彩评论

暂无评论...
验证码 换一张
取 消