Definition and Use of Constants, Fields, Attributes and Methods of Global Scope in C#

Keywords: C# Attribute Programming

scene

In development, there are often constants, fields, attributes, methods of global scope.

These need to be set to global scope to save and have unique instances.

Note:

Blog Home Page:
https://blog.csdn.net/badao_liumang_qizhi
Focus on the Public Number
A hegemonic programmed ape
Get programming related e-books, tutorials and free downloads.

Realization

First, create a new global class with a random name, called Global.

 public class Global
        {
            
        }

To ensure its singleton implementation, set the following in the class

 private static string _lockFlag = "GlobalLock";
        private static Global _instance;
        private Global()
        {

        }

        public static Global Instance
        {
            get
            {
                lock (_lockFlag)
                {
                    if (_instance == null)
                    {
                        _instance = new Global();
                    }
                    return _instance;
                }
            }
        }

Global Constant Implementation

public const int INDENT = 5;

public const string FONT_FAMILY = "Song style";

Global field implementation

private string _currCompareDataFile;

private List<DataTreeNode> _compareData = new List<DataTreeNode>();

Global Attribute Implementation

public string CurrCompareDataFile
        {
            get { return _currCompareDataFile; }
            set { _currCompareDataFile = value; }
        }
 public List<DataTreeNode> CompareData
        {
            get {
                return _compareData; 
                }
            set { _compareData = value; }
        }

 

Note:

Global fields are used in conjunction with attributes, which are declared above and set below.

If there are special settings in value or assignment, you can also

public string CurrChartTitle
        {
            get
            {
                if (String.IsNullOrEmpty(this._currDataFile))
                {
                    return "Default title";
                }
                else
                {
                    return System.IO.Path.GetFileNameWithoutExtension(String.Format("{0}{1}", this._currDataFile, Global.MAIN_EXT));
                }
            }
        }

 

Global Method Implementation

 public void Init()
        {
            
        }

Use examples

Constant usage

Global. Constant name

Global.XAXIS_ATTRIBUTE_XPATH

Field usage

Fields are generally used in combination with attributes in Global.

public string CurrCompareDataFile
        {
            get { return _currCompareDataFile; }
            set { _currCompareDataFile = value; }
        }

Attribute usage

Global.Instance.CurrCompareDataFile

Use of methods

Global.Instance.Init();

Posted by Highlander on Mon, 14 Oct 2019 07:09:55 -0700