C# 中自定义字段名的命名规范如下:

  1. 字节长度不超过 20。
  2. 字段名首字母只能为大小写字母或下划线 '_'.
  3. 字段名中除了大小写字母、数字和下划线外可以含有汉字。

实现校验功能的代码示例如下:

public static bool IsValidFieldName(string fieldName)
{
    if (fieldName.Length > 20)
    {
        return false;
    }
    if (!char.IsLetter(fieldName[0]) && fieldName[0] != '_')
    {
        return false;
    }
    foreach (char c in fieldName)
    {
        if (!char.IsLetterOrDigit(c) && c != '_')
        {
            if (!IsChinese(c))
            {
                return false;
            }
        }
    }
    return true;
}

private static bool IsChinese(char charCode)
{
    return charCode >= 0x4e00 && charCode <= 0x9fa5;
}

其中,IsChinese 方法用于判断字符是否为汉字。

C# 自定义字段名规范:字节长度、首字母、字符限制

原文地址: https://www.cveoy.top/t/topic/luNU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录