在C#开发中,编码规范是一种约定,用于规范化代码的书写风格、命名规则和代码组织方式,以提高代码的可读性、可维护性和一致性。
public class CustomerService
{
// ...
}
public interface ILogger
{
// ...
}
public struct Point3D
{
// ...
}
public class Circle
{
public double CalculateArea()
{
// ...
}
public double Radius { get; set; }
public event EventHandler Clicked;
}
private int itemCount;
private string customerName;
public const int MAX_ITEMS = 100;
public const string DEFAULT_NAME = "John Doe";
public void DoSomething()
{
if (condition)
{
// ...
}
else
{
// ...
}
}
int result = a + b;
bool isValid = x > y && z <= 100;
public class MyClass
{
private int field1;
private string field2;
public void Method1()
{
// ...
}
public void Method2()
{
// ...
}
}
// 计算圆的面积
public double CalculateArea(double radius)
{
// ...
}
/// <summary>
/// 执行某个操作。
/// </summary>
/// <param name="value">输入值。</param>
public void DoSomething(int value)
{
// ...
}
/// <summary>
/// 计算圆的面积。
/// </summary>
/// <param name="radius">圆的半径。</param>
/// <returns>圆的面积。</returns>
public double CalculateArea(double radius)
{
// ...
}
try
{
// 可能引发异常的代码
}
catch (Exception ex)
{
// 异常处理逻辑
}
try
{
// 可能引发异常的代码
}
catch (ArgumentNullException ex)
{
// 处理参数为空的异常
}
catch (InvalidOperationException ex)
{
// 处理无效操作的异常
}
catch (Exception ex)
{
// 处理其他异常
}
public class MyClass
{
// 字段
private int field1;
private string field2;
// 属性
public int Property1 { get; set; }
public string Property2 { get; set; }
// 构造函数
public MyClass()
{
// ...
}
// 方法
public void Method1()
{
// ...
}
public void Method2()
{
// ...
}
// 事件
public event EventHandler SomethingHappened;
}
public class MyBaseClass
{
public virtual void MyMethod()
{
// ...
}
}
public class MyDerivedClass : MyBaseClass
{
public override void MyMethod()
{
// ...
}
}
public class MyBaseClass
{
public virtual void MyMethod()
{
// ...
}
}
public class MyDerivedClass : MyBaseClass
{
public override void MyMethod()
{
// ...
}
}
public const int MaxValue = 100;
private readonly int initialValue = 10;
public static void MyStaticMethod()
{
// ...
}
int result = a + b;
if (condition)
{
// ...
}
public void MyMethod()
{
if (condition1)
{
if (condition2)
{
// ...
}
}
else
{
// ...
}
}
using System;
using System.Collections.Generic;
public class MyClass
{
public void MyMethod()
{
List<int> myList = new List<int>();
// ...
}
}
using System.Collections.Generic;
public class MyClass
{
public void MyMethod()
{
List<int> myList = new List<int>();
// ...
}
}
string myString = string.Empty;
string name = "John";
int age = 30;
// 使用 string.Format
string message = string.Format("My name is {0} and I'm {1} years old.", name, age);
// 使用插值字符串
string message = $"My name is {name} and I'm {age} years old.";
int[] numbers = new int[] { 1, 2, 3, 4, 5 };
List<string> names = new List<string>() { "John", "Jane", "Bob" };
public async Task<int> CalculateSumAsync()
{
// ...
}
public async Task DoSomethingAsync()
{
// 异步操作
await SomeAsyncMethod();
// ...
}
var query = from person in people
where person.Age > 18
select person.Name;
var query = people.Where(person => person.Age > 18)
.Select(person => person.Name);
var adults = people.Where(person => person.Age > 18);
var names = adults.Select(person => person.Name);
[TestFixture]
public class MyTestClass
{
[Test]
public void MyTestMethod()
{
// 测试逻辑
}
}
[Test]
public void CalculateSum_ShouldReturnCorrectValue()
{
// 测试逻辑
}
try
{
// 代码块
}
catch (SpecificException ex)
{
// 处理特定异常
}
catch (AnotherException ex)
{
// 处理另一种异常
}
try
{
// 代码块
}
catch (SpecificException ex)
{
// 记录异常信息
logger.Log(ex);
// 执行适当的错误处理
ShowErrorMessage("An error occurred. Please try again later.");
}
string directoryPath = @"C:\MyDirectory";
string filePath = Path.Combine(directoryPath, "myfile.txt");
if (File.Exists(filePath))
{
// 处理文件逻辑
}
logger.LogInformation("Processing started.");
try
{
// 代码块
}
catch (Exception ex)
{
logger.LogError(ex, "An error occurred during processing.");
}
// 计算两个整数的和
int result = a + b;
// 下面是一个循环
for (int i = 0; i < 10; i++)
{
// ...
}
namespace MyProject.Utilities
{
public class Helper
{
// ...
}
}
MyClass.cs