C# 代码风格检查报告

生成时间: 2025-04-29 16:01:35

检查文件: 1 个

问题摘要

总问题数
18
错误
0
警告
17
提示
1

文件摘要

文件 问题数 错误 警告 提示
StyleRulesTest.cs 18 0 17 1

问题详情

StyleRulesTest.cs

example\StyleRulesTest.cs
规则严重性消息
7 18 CS0002 warning 接口名 'resource' 必须以'I'开头
21 15 CSN003 warning 结构体名 'Point' 必须以'st'开头
35 14 CSN001 warning 类名 'badPlayer' 应该使用PascalCase命名法(首字母大写)
35 24 CSS001 warning 'class' 的开括号应该放在新行
37 22 CS0007 warning 常量名 'maxPlayers' 应该全部大写并使用下划线分隔单词
38 24 CS0007 warning 常量名 'DefaultSpeed' 应该全部大写并使用下划线分隔单词
45 17 CSN005 warning 私有字段 'health' 应该使用下划线前缀
46 19 CSN005 warning 私有字段 'speed' 应该使用下划线前缀
53 24 CS0010 warning 静态字段 'playerCount' 应该以's_'开头
61 17 CSN004 warning 方法名 'updateHealth' 应该使用PascalCase命名法(首字母大写)
66 26 CSN005 warning 私有字段 'item' 应该使用下划线前缀
66 26 CS0009 info 集合类型字段 'item' 的命名建议使用复数形式
87 19 CSS001 warning 'if' 的开括号应该放在新行
132 30 CSS001 warning 'for' 的开括号应该放在新行
137 28 CSS001 warning 'foreach' 的开括号应该放在新行
142 5 CSS001 warning 'try' 的开括号应该放在新行
144 24 CSS001 warning 'catch' 的开括号应该放在新行
146 11 CSS001 warning 'finally' 的开括号应该放在新行

代码预览


1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Linq;
5
6// 错误的接口命名(不以I开头)
7public interface resource
8{
9    void LoadResource();
10    void UnloadResource();
11}
12
13// 正确的接口命名
14public interface IResource
15{
16    void LoadResource();
17    void UnloadResource();
18}
19
20// 错误的结构体命名(不以st开头)
21public struct Point
22{
23    public int X;
24    public int Y;
25}
26
27// 正确的结构体命名
28public struct stPoint
29{
30    public int X;
31    public int Y;
32}
33
34// 错误的类名(不符合PascalCase)
35public class badPlayer {  // 同时违反了大括号规则
36    // 常量命名错误(不是全大写)
37    public const int maxPlayers = 4;
38    public const float DefaultSpeed = 5.0f;
39    
40    // 正确的常量命名
41    public const int MAX_PLAYERS = 4;
42    public const float MAX_SPEED = 10.0f;
43    
44    // 私有字段命名错误(没有下划线前缀)
45    private int health;
46    private float speed;
47    
48    // 正确的私有字段命名
49    private int _health;
50    private float _speed;
51    
52    // 静态字段命名错误(没有使用s_+类型缩写格式)
53    private static int playerCount;
54    private static Dictionary<int, string> playerNames;
55    
56    // 正确的静态字段命名
57    private static int s_iPlayerCount;
58    private static Dictionary<int, string> s_dictPlayerNames;
59    
60    // 方法命名错误(不符合PascalCase)
61    public void updateHealth(int amount) {  // 同时违反了大括号规则
62        _health += amount;
63    }
64    
65    // 集合命名错误(不是复数形式)
66    private List<string> item;
67    private Dictionary<int, float> scoreMap;
68    
69    // 正确的集合命名
70    private List<string> _items;
71    private Dictionary<int, float> _scoreMaps;
72    
73    // 行过长示例(视具体设置的最大行长而定)
74    public void SomeLongMethod() {
75        Console.WriteLine("这是一行非常长的代码,它故意超出了规则中设置的最大行长度限制,目的是测试行长度检查规则是否能正确工作。我们期望在代码检查时这一行会被标记为违反了LineIsTooLongRule规则。");
76    }
77    
78    // 正确的方法命名和大括号放置
79    public void UpdateHealth(int amount)
80    {
81        _health += amount;
82    }
83    
84    public void Update()
85    {
86        // if语句的大括号错误放置
87        if (_health <= 0) {
88            Console.WriteLine("Game Over");
89        }
90        
91        // 正确的if语句大括号放置
92        if (_speed > MAX_SPEED)
93        {
94            _speed = MAX_SPEED;
95        }
96    }
97}
98
99// 正确的类名
100public class Player
101{
102    public void Move(float x, float y)
103    {
104        // 实现移动逻辑
105    }
106}
107
108// 测试枚举命名
109public enum direction  // 错误的枚举命名(不符合PascalCase)
110{
111    North,
112    South,
113    East,
114    West
115}
116
117// 正确的枚举命名
118public enum Direction
119{
120    North,
121    South,
122    East,
123    West
124}
125
126// 包含各种嵌套块的示例类
127public class StyleTestClass
128{
129    public void TestMethod()
130    {
131        // 错误的for循环大括号
132        for (int i = 0; i < 10; i++) {
133            Console.WriteLine(i);
134        }
135        
136        // 错误的foreach循环大括号
137        foreach (var item in new[] { 1, 2, 3 }) {
138            Console.WriteLine(item);
139        }
140        
141        // 错误的try-catch大括号
142        try {
143            throw new Exception();
144        } catch (Exception ex) {
145            Console.WriteLine(ex.Message);
146        } finally {
147            Console.WriteLine("Finally");
148        }
149    }
150}