开发——硬编码

可变变量的硬编码

声明static类赋值,或者添加config文件

You can use .config files or use constant values in a static class. If you don't need to change the values, I would suggest using constant values.

Example using constant values:

For example:

public static class Roles
{
  public const string Customer = "Customer";
  public const string Branch = "Branch";
} 

Usage:

if (role == Roles.Customer)
{

}
else if (role == Roles.Branch)
{

}

  

Example using config file:

    • Add a reference to System.Configuration
    • Add an "Application Configuration File" to your project
    • Add a configuration key to the configuration file like:
      <?xmlversion="1.0"encoding="utf-8"
      ?>
      <
      configuration
      >
         <
      appSettings
      >
            <
      addkey="myConfiguration"value="TestValue"
      />
         </
      appSettings
      >
      </
      configuration
      >
    • Within the code you can refer to the config file by using:
      string configValue = System.Configuration.ConfigurationManager.AppSettings["myConfiguration"];

使用枚举类

use an enum to help with readability.

enum MyTypes
{
 URL = 1,
 UserName = 2,
 Id = 3,
}

switch (myType)
{
    case MyTypes.URL:
        TidyUrl();
    case MyTypes.UserName:
        TidyUsername();
    case MyTypes.Id:
        TidyID();
    default:
        break;
}

  

原文地址:https://www.cnblogs.com/panpanwelcome/p/7356736.html