A collection of Roslyn analyzers and code fixes for GameShowPro projects to improve code quality and maintainability.
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.
dotnet add package GameShowPro.AnalyzersOr via Package Manager:
Install-Package GameShowPro.Analyzers
Download and install the VSIX extension from the Visual Studio Marketplace or build it from source.
The analyzer identifies properties that:
- Have explicit backing fields with common naming patterns (
_propertyName,_PropertyName, orpropertyName) - Only use the backing field in their get/set/init accessors
- Are simple pass-through implementations (no additional logic)
- 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; }
}The code fix performs the following transformations:
- Converts property accessors to use
fieldkeyword - Removes the backing field declaration
- Preserves field initializers as property initializers (if not default values)
- Replaces all direct field references with property references
Note: The code fix requires a compiler that supports the
fieldkeyword (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.
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++;
}Before:
private int _count = 42;
public int Count
{
get { return _count; }
set { _count = value; }
}After:
public int Count
{
get => field;
set => field = value;
} = 42;Before:
private readonly string _name = "Default";
public string Name
{
get { return _name; }
}After:
public string Name
{
get => field;
} = "Default";Before:
private string _name;
public string Name
{
get => _name;
set => _name = value;
}After:
public string Name
{
get => field;
set => field = value;
}- 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
fieldkeyword, which requires C# 13.0+ - Roslyn 3.3.1+ - The analyzer is built using Microsoft.CodeAnalysis.CSharp version 3.3.1
- Diagnostic ID:
GSP0001 - Category: Usage
- Severity: Info
- Enabled by default: Yes
The UseFieldKeyword analyzer recognizes the following backing field naming patterns:
_propertyName(underscore + camelCase) - most common_PropertyName(underscore + PascalCase)propertyName(camelCase without underscore)
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
Using the field keyword provides several advantages:
- Less boilerplate code - No need to declare separate backing fields
- Cleaner code - Property implementation is more concise
- Better encapsulation - The backing field is implicitly private to the property
- Modern C# style - Uses the latest language features
- Reduced surface area - Fewer symbols in the type's member list
- Less error-prone - Cannot accidentally expose the backing field
- Visual Studio 2022 or later
- .NET SDK 6.0 or later
- Visual Studio SDK (for VSIX project)
# 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.csprojThis 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
Contributions are welcome! Please feel free to submit issues and pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.
Copyright � 2025 Barjonas LLC
For issues, questions, or contributions, please visit the GitHub repository.