什么时候应该使用公共/私营/静态方法? [英] When should I use public/private/static methods?

查看:197
本文介绍了什么时候应该使用公共/私营/静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C#.Till这一刻我用来做每一个全局变量 - 公共static.All我的方法是公共静态,所以我可以从其他类访问它们。

I'm new to C#.Till this moment I used to make every global variable - public static.All my methods are public static so I can access them from other classes.

我因此,阅读的较少公共静态方法我有,在better.So我重写我的应用程序通过把所有的代码在一个类 - 形式class.Now我所有的方法都是私人和没有静态方法。

I read on SO that the less public static methods I have,the better.So I rewrote my applications by putting all the code in one class - the form class.Now all my methods are private and there's no static method.

我的问题:我应该怎么做,藏在心里形式类是转储在我看来,

My question: What should I do,keeping everything in the form class is dump in my opinion.

什么时候应该使用公共,当私人和静止时公/私?

When should I use public,when private and when static private/public?

我得到的公共方法作为一个'缺点',因为它们可以反编译,但我怀疑that.My公共方法可以反编译too.What是如此的私人的私有方法

I get the public methods as a 'cons' ,because they can be decompiled,but I doubt that.My public methods can be decompiled too.What is so 'private' in a private method?

修改:我不要求如何防止我的程序进行反编译,我问我是否应该使用静态的,民营和public.And也:是否有在把所有的代码形式类,所以我不必使用公共方法问题

EDIT: I'm not asking how to prevent my program to be decompiled,I'm asking whether I should use static,private and public.And also : Is there are problem in putting all the code in the form class so I dont have to use public methods?

推荐答案

私人是您想要的类人体内只能访问类的成员,并在C#中的成员都是默认设置为私有,除非

private is for class members that you want only access within the class of the body, and in C# members are default set to private unless specified different

examples of when to use private:

例子
{

私人INT引脚= 1090;
公共INT引脚
{
{返回针; }
}
}



另一方面公众是相反的,有与访问公共成员没有限制,所以当事情不与有机会获得用户的问题应该是公共的。

public on the other hand is the opposite, there are no restrictions with accessing public members, so when things that don't matter with the user having access to should be public.

静电,另一方面没有任何关系两个,因为它不与权限的方法处理,静态另一方面是一个常数或类型声明。

static on the other hand has no relation to the two, because it doesn't deal with permission to methods, static on the other hand is a constant or type declaration. If the word static is applied to the class then every member in the class must be declared static.

例子

  static int birth_year= 1985

在C#参考修饰符会给你所有的调节器的更详细的C#和他们应该如何实例使用

Modifiers in C# Reference will give you more detail of all modifiers in C# and examples of how they should be used

这篇关于什么时候应该使用公共/私营/静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆