Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

[Feature] Extend Guard Methods to Return Validated Arguments

未关闭
#1,110 0 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
csharp

调研方向

首先检查现有的 Guard API 及其方法,然后比较提议的 ResultGuard 和 modified-Guard 方案。与维护者确认预期的返回行为和 API 兼容性;完成的标准是,达成一致的 Guard 方法返回经过验证的参数,同时不改变验证失败行为或现有用法。

由索引模型根据 Issue 内容生成。

描述

Proposal: Extend Guard Methods to Return Validated Arguments

Summary

Extend the existing Guard functionality to provide an alternative version of all guard methods that return the validated argument when the validation passes. This enhancement would allow method chaining and inline validation assignments.

Current Usage

The current Guard methods validate an argument and throw an exception if the validation fails, but they return void. This requires an extra assignment step when the argument is needed after validation.

Example:

public class Processor
{
    private readonly string _input;

    public Processor(string input)
    {
        Guard.IsNotNullOrEmpty(input);
        _input = input;
    }

    public void Print()
    {
        Console.WriteLine(_input);
    }
}

Proposed Enhancement

Provide an alternative version of all guard methods that return the validated argument when the validation passes. This would allow inline validation while preserving the original API behavior.

Proposed Usage

Instead of requiring a separate assignment, the new API would allow:

public class Processor
{
    private readonly string _input;

    public Processor(string input)
    {
        _input = Guard.IsNotNullOrEmpty(input);
    }

    public void Print()
    {
        Console.WriteLine(_input);
    }
}

Implementation Options

Two approaches could be taken to introduce this feature:

Option 1: Create a ResultGuard Class

Introduce a new ResultGuard class that mirrors Guard but returns the validated argument.

public static class ResultGuard
{
    public static T IsNotNullOrEmpty<T>(T value, [CallerArgumentExpression("value")] string? paramName = null)
        where T : class
    {
        Guard.IsNotNullOrEmpty(value, paramName);
        return value;
    }
}

Pros:

  • No changes to the existing Guard class.
  • Clear separation between validation-only (Guard) and validation-with-return (ResultGuard).

Cons:

  • Code duplication or the need to refactor Guard to reuse logic.
  • Users must choose between Guard and ResultGuard.

Option 2: Modify Guard to Return the Argument

Modify the existing Guard class to return the argument instead of void.

public static class Guard
{
    public static T IsNotNullOrEmpty<T>(T value, [CallerArgumentExpression("value")] string? paramName = null)
        where T : class
    {
        if (string.IsNullOrEmpty(value))
        {
            throw new ArgumentException($"{paramName} cannot be null or empty.", paramName);
        }
        return value;
    }
}

Pros:

  • No need for a new class.
  • Maintains a single validation API.
  • Fully backward-compatible since it only extends the return type.

Cons:

  • Changes the return type of existing methods, which may have unforeseen consequences in certain use cases.

Personal Preference

I personally prefer Option 2 (modifying Guard) because it avoids duplication, maintains backward compatibility, and enhances usability with minimal changes. However, I am open to other suggestions if the maintainers have concerns or alternative approaches in mind.

Would the maintainers be open to this enhancement? If so, I can submit a PR with the implementation.

主要语言
C#
星标
3.8k
派生
403
PR 合并指标
30 天内没有已合并 PR

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

CommunityToolkit/dotnet 的其他 Issue

查看 CommunityToolkit/dotnet 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。