这是我上午发的帖子http://topic.csdn.net/u/20090427/11/288fc390-9df4-46a8-b628-70a7782b94f2.html
本来只是问一下在声明方法时什么时候用static比较合适。
但是看过各位的回复后感觉更迷茫了。
静态变量我知道怎么回事,也知道什么时候用合适。但对于静态方法就有些迷茫。1.关于static方法内存分配的问题?如何分配内存?
是不是一加载应用程序就给所有的static方法分配内存,那system命名空间里很多static方法是不是占去很大内存?
http://topic.csdn.net/u/20090427/11/288fc390-9df4-46a8-b628-70a7782b94f2.html17楼2.static方法比非static方法更占资源吗?3.static方法和非static方法哪个效率高?都一样?4.声明方法时,何时声明称static方法更好?
本来只是问一下在声明方法时什么时候用static比较合适。
但是看过各位的回复后感觉更迷茫了。
静态变量我知道怎么回事,也知道什么时候用合适。但对于静态方法就有些迷茫。1.关于static方法内存分配的问题?如何分配内存?
是不是一加载应用程序就给所有的static方法分配内存,那system命名空间里很多static方法是不是占去很大内存?
http://topic.csdn.net/u/20090427/11/288fc390-9df4-46a8-b628-70a7782b94f2.html17楼2.static方法比非static方法更占资源吗?3.static方法和非static方法哪个效率高?都一样?4.声明方法时,何时声明称static方法更好?
例,将某数字转换为它的绝对值.
i=Math.Abs(i);
Abs是Math类中的静态方法,所以可以这么做.
如果Abs不是静态方法,你需要:
Math m=new Math();
i=m.Abs(i);
很明显,将Abs设为静态方法更方便.
是一加载应用程序就给所有的static方法分配内存,那system命名空间里很多static方法是不是占去很大内存
一般每个方法的内存都很小。
2.static方法比非static方法更占资源吗?
一样。
3.static方法和非static方法哪个效率高?都一样?
static方法效率高,常驻内存。4.声明方法时,何时声明称static方法更好?
需要全局共享的声明为static
如果非需要全共享时,建议你不要用 static因为常驻内存,多少还是占用资源的。
static方法或类只有在类的第一次访问时才会加载;2.static方法比非static方法更占资源吗?
都一样 无任是静态方法还是实例方法都在内存中都共用一个副本.3.static方法和非static方法哪个效率高?都一样?
执行起来都是一样的.,只是有时候不必为了调用一个方法而去创建一个实例4.声明方法时,何时声明称static方法更好?
当这个方法中不需要操作自己类实例成员的数据时可以静态化,方便调用.一旦static,说明这个方法与类中实例成员无关
{
public static void AddStudent(Student stu)
{
....
}
public static List<Student> GetAllStudent()
{
....
} public static Student GetStudentById()
{
....
}
}
{
public static string GetCompanyName(string name) { ... }
public static string GetCompanyAddress(string address) { ... }
}
有关更多信息,请参见静态类和静态类成员(C# 编程指南)。 备注
常数或者类型声明隐式地是静态成员。不能通过实例引用静态成员。然而,可以通过类型名称引用它。例如,请考虑以下类:public class MyBaseC
{
public struct MyStruct
{
public static int x = 100;
}
}
若要引用静态成员 x,请使用完全限定名(除非可从相同范围访问):MyBaseC.MyStruct.x
尽管类的实例包含该类所有实例字段的单独副本,但每个静态字段只有一个副本。不可以使用 this 来引用静态方法或属性访问器。如果对类应用 static 关键字,则该类的所有成员都必须是静态的。类(包括静态类)可以有静态构造函数。在程序开始和实例化类之间的某个时刻调用静态构造函数。注意
static 关键字在使用上比在 C++ 中有更多的限制。若要与 C++ 关键字比较,请参见 static。为了说明静态成员,请看一个表示公司雇员的类。假设该类包含一种对雇员计数的方法和一个存储雇员数的字段。该方法和字段都不属于任何实例雇员,而是属于公司类。因此,应该将它们声明为此类的静态成员。 示例
该示例读取新雇员的名称和 ID,逐个增加雇员计数器并显示新雇员的有关信息以及新的雇员数。为简单起见,该程序从键盘读取当前的雇员数。在实际的应用中,应从文件读取此信息。// cs_static_keyword.cs
using System;public class Employee
{
public string id;
public string name; public Employee()
{
} public Employee(string name, string id)
{
this.name = name;
this.id = id;
} public static int employeeCounter; public static int AddEmployee()
{
return ++employeeCounter;
}
}class MainClass : Employee
{
static void Main()
{
Console.Write("Enter the employee's name: ");
string name = Console.ReadLine();
Console.Write("Enter the employee's ID: ");
string id = Console.ReadLine(); // Create and configure the employee object:
Employee e = new Employee(name, id);
Console.Write("Enter the current number of employees: ");
string n = Console.ReadLine();
Employee.employeeCounter = Int32.Parse(n);
Employee.AddEmployee(); // Display the new information:
Console.WriteLine("Name: {0}", e.name);
Console.WriteLine("ID: {0}", e.id);
Console.WriteLine("New Number of Employees: {0}",
Employee.employeeCounter);
}
}
输入Tara Strahan
AF643G
15
示例输出Enter the employee's name: Tara Strahan
Enter the employee's ID: AF643G
Enter the current number of employees: 15
Name: Tara Strahan
ID: AF643G
New Number of Employees: 16
此示例显示:虽然可以用另一个尚未声明的静态字段实例化一个静态字段,但只要给静态字段显式赋值后,结果才是确定的。// cs_static_keyword_2.cs
using System;
class Test
{
static int x = y;
static int y = 5; static void Main()
{
Console.WriteLine(Test.x);
Console.WriteLine(Test.y); Test.x = 99;
Console.WriteLine(Test.x);
}
}
输出0
5
99
非Static函数的类是个控件,构造之后就独立,随便赋值随便用。
纯属个人理解。
up
static 是指静态的,与实例无关的.那些做为类级别的特性的方法,字段等需要设计为静态的.
反之,如果是与实例相关的则不应当设计为静态.