static, readonly, const

  • static

    • Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object. The static modifier
      can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes.
    • A constant or type declaration is implicitly a static member.
    • A static member cannot be referenced through an instance. Instead, it is referenced through the type name.
    • While an instance of a class contains a separate copy of all instance fields of the class, there is only one copy of each static field.
    • It is not possible to use this to reference static methods or property accessors.
    • If the static keyword is applied to a class, all the members of the class must be static.
    • Classes and static classes may have static constructors. Static constructors are called at some point between when the program starts and the class is instantiated.
    • static class is basically the same as a non-static class, but there is one difference: a static class cannot be
      instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because
      there is no instance variable, you access the members of a static class by using the class name itself.
    • A static class can be used as a convenient container for sets of methods that just operate on input parameters and do not have to get or set any internal instance fields.
    • As is the case with all class types, the type information for a static class is loaded by the .NET Framework common language runtime (CLR) when the program that references the class is loaded. The program
      cannot specify exactly when the class is loaded. However, it is guaranteed to be loaded and to have its fields initialized and its static constructor called before the class is referenced for the first time in your program. A
      static constructor is only called one time, and a static class remains in memory for the lifetime of the application domain in which your program resides.
    • static class: Contains only static members, Cannot be instantiated, Is sealed, Cannot contain Instance Constructors.
    • Creating a static class is therefore basically the same as creating a class that contains only static members and a private constructor. A private constructor prevents the class from
      being instantiated. The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee
      that instances of this class cannot be created.
    • Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object. Static
      classes cannot contain an instance constructor; however, they can contain a static constructor. Non-static classes should also define a static constructor if the class contains static members that require non-trivial
      initialization.
    • A non-static class can contain static methods, fields, properties, or events.
    • The static member is always accessed by the class name, not the instance name. Static methods and properties cannot access non-static
      fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.
    • It is more typical to declare a non-static class with some static members, than to declare an entire class as static.
    • Two common uses of static fields are to keep a count of the number of objects that have been instantiated, or to store a value that must be shared among all instances.
    • Static methods can be overloaded but not overridden, because they belong to the class, and not to any instance of the class.
    • Although a field cannot be declared as static const, a const field
      is essentially static in its behavior. It belongs to the type, not to instances of the type.
    • Static members are initialized before the static member is accessed for the first time and before the static constructor, if there
      is one, is called.
    • C# does not support static local variables (variables that are declared in method scope).
  • const
    • The static modifier is not allowed in a constant declaration.
    • A const field can only be initialized at the declaration of the field. A readonly field can be initialized
      either at the declaration or in a constructor. Therefore,readonly fields can have different values depending on the constructor used. Also,
      although a const field is a compile-time constant, the readonly field can be used for run-time constants, as in this line: public static readonly uint l1 = (uint)DateTime.Now.Ticks;

Examples:

   public static class MyStaticClass : MyClass // error CS0713: Static classes must derive from object.
   {
      static MyStaticClass()
      {
         Console.WriteLine("MyStaticClass constructor");
      }
      public static void Test() { Console.WriteLine("MyStaticClass:Test"); }
   }

   public class Derived : MyStaticClass { } //'Derived': cannot derive from static class 'MyStaticClass'

static, readonly, const

时间: 2024-12-13 09:36:20

static, readonly, const的相关文章

【转】const和static readonly

我们都知道,const和static readonly的确很像:通过类名而不是对象名进行访问,在程序中只读等等.在多数情况下可以混用.二者本质的区别在于,const的值是在编译期间确定的,因此只能在声明时通过常量表达式指定其值.而static readonly是在运行时计算出其值的,所以还可以通过静态构造函数来赋值.明白了这个本质区别,我们就不难看出下面的语句中static readonly和const能否互换了: 1. static readonly MyClass myins = new M

const和static readonly 区别

const的值是在编译期间确定的,因此只能在声明时通过常量表达式指定其值. 而static readonly是在运行时计算出其值的,所以还可以通过静态构造函数来赋值. static readonly MyClass myins = new MyClass();(对) static readonly MyClass myins = "3";(对) const string myins = "3";(对) const MyClass myins = new MyClas

浅谈C#中const、static readonly区别

const 编译时常量 static readonly 运行时常量 直接上代码 1.新建一个类库BLL->添加类Teacher 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace BLL 8 { 9 public class Teacher 10 { 11 public

[转]C# const和static readonly区别

我们都知道,const和static readonly的确很像:通过类名而不是对象名进行访问,在程序中只读等等.在多数情况下可以混用.二者本质的区别在于,const的值是在编译期间确定的,因此只能在声明时通过常量表达式指定其值.而static readonly是在运行时计算出其值的,所以还可以通过静态构造函数来赋值.明白了这个本质区别,我们就不难看出下面的语句中static readonly和const能否互换了: 1. static readonly MyClass myins = new M

(C#) What is the difference between "const" and "static readonly" ?

const int a must be initialized initialization must be at compile time readonly int a can use default value, without initializing initialization can be at run time 二者本质的区别在于,const的值是在编译期间确定的,因此只能在声明时通过常量表达式指定其值.而static readonly是在运行时计算出其值的,所以还可以通过静态构造

【Unity|C#】基础篇(6)——const、readonly、static readonly

[学习资料] <C#图解教程>(第6章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu.com/s/1mhOmBG0 [内容] const readonly static readonly 三者比较 [笔记] const 编译时常量 类的常量 只能在声明时赋值 readonly 运行时常量 对象的常量 可以在 声明时 赋值(与常量一样),或 构造函数中 赋值 static readonly 运行时

readonly const关键字

readonly 关键字与 const 关键字不同. const 字段只能在该字段的声明中初始化. readonly 字段可以在声明或构造函数中初始化. 因此,根据所使用的构造函数,readonly 字段可能具有不同的值. 另外,const 字段为编译时常数,而 readonly 字段可用于运行时常数,如下例所示: public static readonly uint timeStamp = (uint)DateTime.Now.Ticks; // 运行时的时间赋值给static readon

PHP 面向对象中常见关键字使用(final、static、const和instanceof)

PHP 面向对象中常见关键字的使用: 00x1.Final :final关键字可以加在类或者类中方法之前,但是不能使用final标识成员属性. 作用: 使用final标识的类,不能被继承. 在类中使用final标识的成员方法,在子类中不能覆盖. 总结:final表示为最终的意思,所以使用final关键字的类或者类中的成员方法是不能被更改的. 00x2.Static :static关键字将类中的成员属性或者成员方法标识为静态的,static标识的成员属性属于整个类,static成员总是唯一存在的,

static和extern使用 /static和const联合使用

static: 1.修饰局部变量,被static修饰局部变量,延长生命周期,跟整个应用程序有关 * 被static修饰局部变量,只会分配一次内存 * 被static修饰局部变量什么分配内存? 程序一运行就会给static修饰变量分配内存 2.修饰全局变量,被static修饰全局变量,作用域会修改,只能在当前文件下使用 extern:声明外部全局变量,注意:extern只能用于声明,不能用于定义 extern工作原理:先会去当前文件下查找有没有对应全局变量,如果没有,才回去其他文件查找 exter