Skip to content

A collection of Roslyn analyzers and code fixes to improve code quality and maintainability.

License

Notifications You must be signed in to change notification settings

gameshowpro/Analyzers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

GameShowPro.Analyzers

A collection of Roslyn analyzers and code fixes for GameShowPro projects to improve code quality and maintainability.

Analyzers Included

UseFieldKeyword Analyzer

This analyzer detects properties with backing fields and provides a code fix to refactor them to use the C# field keyword instead. The field keyword was introduced in C# 13.0 (.NET 9) and simplifies property implementations by eliminating the need for explicit backing fields.

Installation

NuGet Package

dotnet add package GameShowPro.Analyzers

Or via Package Manager:

Install-Package GameShowPro.Analyzers

VSIX Extension

Download and install the VSIX extension from the Visual Studio Marketplace or build it from source.

UseFieldKeyword Analyzer

What It Does

The analyzer identifies properties that:

  1. Have explicit backing fields with common naming patterns (_propertyName, _PropertyName, or propertyName)
  2. Only use the backing field in their get/set/init accessors
  3. Are simple pass-through implementations (no additional logic)
  4. Are compiled with C# 8.0 or higher (to ensure modern language features)

Example code that triggers the analyzer:

private int _count;

public int Count
{
    get { return _count; }
    set { _count = value; }
}

Code Fix

The code fix performs the following transformations:

  1. Converts property accessors to use field keyword
  2. Removes the backing field declaration
  3. Preserves field initializers as property initializers (if not default values)
  4. Replaces all direct field references with property references

Note: The code fix requires a compiler that supports the field keyword (C# 13.0+). The analyzer will detect the pattern in any C# 8.0+ code, but applying the fix requires upgrading to C# 13.0 or later.

Example Transformations

Basic Property

Before:

private int _count;

public int Count
{
    get { return _count; }
    set { _count = value; }
}

public void Increment()
{
    _count++;
}

After:

public int Count
{
    get => field;
    set => field = value;
}

public void Increment()
{
    Count++;
}

With Initializers

Before:

private int _count = 42;

public int Count
{
    get { return _count; }
    set { _count = value; }
}

After:

public int Count
{
    get => field;
    set => field = value;
} = 42;

Read-Only Properties

Before:

private readonly string _name = "Default";

public string Name
{
    get { return _name; }
}

After:

public string Name
{
    get => field;
} = "Default";

Expression-Bodied Accessors

Before:

private string _name;

public string Name
{
    get => _name;
    set => _name = value;
}

After:

public string Name
{
    get => field;
    set => field = value;
}

Requirements

  • Analyzer: C# 8.0 or higher - The analyzer activates when the project targets C# 8.0+
  • Code Fix: C# 13.0 or higher - The code fix produces code using the field keyword, which requires C# 13.0+
  • Roslyn 3.3.1+ - The analyzer is built using Microsoft.CodeAnalysis.CSharp version 3.3.1

Diagnostic Information

GSP0001 UseFieldKeyword

  • Diagnostic ID: GSP0001
  • Category: Usage
  • Severity: Info
  • Enabled by default: Yes

Supported Patterns

The UseFieldKeyword analyzer recognizes the following backing field naming patterns:

  • _propertyName (underscore + camelCase) - most common
  • _PropertyName (underscore + PascalCase)
  • propertyName (camelCase without underscore)

What the Analyzer Does NOT Flag

The analyzer will not report diagnostics for:

  • Auto-properties (already optimized)
  • Properties without backing fields
  • Properties with logic in accessors (validation, calculations, etc.)
  • Properties where accessors do more than simple get/set
  • Properties with backing fields that don't match common naming patterns
  • Code compiled with C# versions older than 8.0

Benefits

Using the field keyword provides several advantages:

  1. Less boilerplate code - No need to declare separate backing fields
  2. Cleaner code - Property implementation is more concise
  3. Better encapsulation - The backing field is implicitly private to the property
  4. Modern C# style - Uses the latest language features
  5. Reduced surface area - Fewer symbols in the type's member list
  6. Less error-prone - Cannot accidentally expose the backing field

Building from Source

Prerequisites

  • Visual Studio 2022 or later
  • .NET SDK 6.0 or later
  • Visual Studio SDK (for VSIX project)

Build Steps

# Clone the repository
git clone https://github.com/GameShowPro/Analyzers.git
cd Analyzers

# Build the solution
dotnet build src\GameShowPro.Analyzers.slnx

# Run tests
dotnet test src\Test\GameShowPro.Analyzers.Test.csproj

# Pack the NuGet package
dotnet pack src\Package\GameShowPro.Analyzers.Package.csproj

Future Analyzers

This package is designed to be extended with additional analyzers for GameShowPro projects. Future analyzers may include:

  • Code quality checks specific to game show applications
  • Performance optimizations for real-time scenarios
  • Best practices enforcement for multimedia handling
  • Thread safety analyzers for concurrent operations

Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Copyright

Copyright � 2025 Barjonas LLC

Support

For issues, questions, or contributions, please visit the GitHub repository.

About

A collection of Roslyn analyzers and code fixes to improve code quality and maintainability.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published