# 1. static keyword

Next, I designed a floating class of mortgage interest rate (to calculate how much the mortgage interest rate has risen):

``` 1 public class InterestRateGoUp
2 {
3     public InterestRateGoUp()
4     {
5         Rate = 4.9m;
6     }
7
8     /// <summary>
9     /// benchmark interest rate
10     /// </summary>
11     public decimal Rate { get; set; }
12
13     /// <summary>
14     /// Computational upstream
15     /// </summary>
16     /// <param name="rate">Current interest rates on mortgages</param>
17     /// <returns>Return the result of interest rate rise</returns>
18     public decimal CalculatorGoUp(decimal rate)
19     {
20         decimal result = (rate - Rate) / Rate;
21         return result;
22     }
23 }
24 public class RealizeObject
25 {
26     public Realize()
27     {
28         //Xiaoxuan Home Purchase Interest Rate 6.37
29         InterestRateGoUp rate1 = new InterestRateGoUp();
30         float result1 = rate1.CalculatorGoUp(6.37m);
31         //Adzuki Bean Home Purchase Interest Rate 6.125
32         InterestRateGoUp rate2 = new InterestRateGoUp();
33         float result2 = rate2.CalculatorGoUp(6.125m);
34         //The benchmark interest rate on mortgages became 5.88，The benchmark interest rates of all the subjects need to be changed
35         rate1.Rate = 5.88m;
36         rate2.Rate = 5.88m;
37         //If there are hundreds or thousands of such objects, they need to be modified hundreds or thousands of times.
38     }
39 }```

The problem with the above example is that the property of the benchmark interest rate is shared by all the floating objects of the mortgage interest rate, not that every floating object of the mortgage interest rate has a benchmark interest rate. So to set the benchmark interest rate as the shared need to use the static keyword, the second version of the mortgage rate floating class:

``` 1 public class InterestRateGoUp
2 {
3     public InterestRateGoUp()
4     {
5         Rate = 4.9m;
6     }
7
8     /// <summary>
9     /// benchmark interest rate
10     /// </summary>
11     public static decimal Rate { get; set; }
12 }
13 public class RealizeObject
14 {
15     public Realize()
16     {
17         //The benchmark interest rate on mortgages became 5.88，All you need to do is modify the static attributes (which are used by all objects), and the use of the static attributes is as follows
18         InterestRateGoUp.Rate = 5.88m;
19         //There is a problem in initializing static attributes with constructors, that is, static attributes are initialized every time an object is created, and the benchmark interest rate is changed to 4 as follows.9
20         InterestRateGoUp rate3 = new InterestRateGoUp();
21     }
22 }```

There are two ways to initialize static automatic attributes. The first is to change static automatic attributes to static attributes (static attributes encapsulate an assigned static private field), the second is to use static constructors, and the third version of the mortgage rate floating class:

``` 1 public class InterestRateGoUp
2 {
3     /// <summary>
4     /// Method 1: Static attributes, not static automatic attributes
5     /// </summary>
6     private static decimal rate = 4.9m;
7     public static decimal Rate
8     {
9         get { return rate; }
10         set { rate = value; }
11     }
12
13     /// <summary>
14     /// Method 2: Static constructor
15     /// </summary>
16     static InterestRateGoUp()
17     {
18         Rate = 4.9m;
19     }
20 }```

Calculator GoUp can be set to static if the method of calculating the float is often used; if the whole mortgage rate float class is a special tool class for users, then all the classes and class members can be set to static. The final version of the mortgage rate floats:

``` 1 public static class InterestRateGoUp
2 {
3     static InterestRateGoUp()
4     {
5         Rate = 4.9m;
6     }
7
8     /// <summary>
9     /// benchmark interest rate
10     /// </summary>
11     public static decimal Rate { get; set; }
12
13     /// <summary>
14     /// Computational upstream
15     /// </summary>
16     /// <param name="rate">Current interest rates on mortgages</param>
17     /// <returns>Return the result of interest rate rise</returns>
18     public static decimal CalculatorGoUp(decimal rate)
19     {
20         decimal result = (rate - Rate) / Rate;
21         return result;
22     }
23 }
24 public class RealizeObject
25 {
26     public Realize()
27     {
28         //Xiaoxuan Home Purchase Interest Rate 6.37
29         float result1 = InterestRateGoUp.CalculatorGoUp(6.37m);
30         //Adzuki Bean Home Purchase Interest Rate 6.125
31         float result2 = InterestRateGoUp.CalculatorGoUp(6.125m);
32         //The benchmark interest rate on mortgages became 5.88
33         InterestRateGoUp.Rate = 5.88m;
34         //Xiaojie Home Purchase Interest Rate 5.88
35         float result3 = InterestRateGoUp.CalculatorGoUp(5.88m);
36     }
37 }```

Summary:

1. static keywords can be modified on class and type members (fields, attributes, methods, constructors, etc.).

2. Static keyword modified content is created only once and allocated memory only once, which is reclaimed at the end of the program (without taking up GC performance). Static keyword modifies type members at the class level (it's unique) rather than at the object level, so static members can only be used (class name. static member) instead of (object name. static member), and static classes can't create their objects using new.

3. Instance fields/attributes are unique to each object. Each object allocates memory space for each instance data. Static fields/attributes are shared by all objects. No matter how many objects are created, static data is allocated only once. Static methods can be used in the case of frequent use, which can reduce the number of memory allocation and cleaning. Class instance fields / attributes and instance methods cannot be used directly in static methods, but only through (object name. XXX). Static constructors are designed for initialization of static data, where they are more suitable for initializing values of static data unknown at compile time (such as read in a database). Static classes, also known as tool classes, are often used throughout the class, cannot be created with new, and can only contain type members modified with static.

Remarks:

1. Static constructors can only define a parametric static constructor and do not allow access modifiers.

2. No matter how many types of objects are created, the static constructor executes only once.

3. Before the runtime creates a class instance or the caller first accesses a static member, the runtime calls the static constructor.

4. The execution of the static constructor precedes that of the non-static constructor.

Part of the content is quoted in the sixth edition of proficient C#.

Tags: C# calculator Database

Posted on Tue, 10 Sep 2019 04:44:35 -0700 by Trs2988