• C# fields Study


    Fields

    A field is a variable that is associated with a class or with an instance of a class.

    A field declared with the static modifier defines a static field. A static field identifies exactly one storage location. No matter how many instances of a class are created, there is only ever one copy of a static field.

    A field declared without the static modifier defines an instance field. Every instance of a class contains a separate copy of all the instance fields of that class.

    In the following example, each instance of the Color class has a separate copy of the r, g, and b instance fields, but there is only one copy of the Black, White, Red, Green, and Blue static fields:

    public class Color
    {
    public static readonly Color Black = new Color(0, 0, 0);
    public static readonly Color White = new Color(255, 255, 255);
    public static readonly Color Red = new Color(255, 0, 0);
    public static readonly Color Green = new Color(0, 255, 0);
    public static readonly Color Blue = new Color(0, 0, 255);

    private byte r, g, b;

    public Color(byte r, byte g, byte b) {
    this.r = r;
    this.g = g;
    this.b = b;
    }
    }

    As shown in the previous example, read-only fields may be declared with a readonly modifier. Assignment to a readonly field can only occur as part of the field’s declaration or in a constructor in the same class.

  • 相关阅读:
    学习HTML-第一章HTML基础
    javascript中apply 和 call 的区别
    数据库
    贝叶斯公式的直观理解(先验概率/后验概率)
    4-决策树
    消息队列
    RESTful api 设计规范
    MySql索引
    MySQL并发控制
    MySQL三层逻辑架构
  • 原文地址:https://www.cnblogs.com/flyinthesky/p/1563420.html
Copyright © 2020-2023  润新知