Code Groups for C# Documentation
CodeGroups are perfect for showing different implementations, overloads, or usage patterns of the same API.
📋 Method Overloads
No Parameters
With Options
With Cancellation
public void Process ()
{
// Process with default settings
ProcessInternal ( DefaultOptions );
}
🔄 Synchronous vs Asynchronous
Synchronous
Asynchronous
Async with ConfigureAwait
public class DataService
{
public string GetData ( int id )
{
var result = _database . Query ( id );
return result . ToString ();
}
public void SaveData ( string data )
{
_database . Insert ( data );
_cache . Invalidate ();
}
}
🎯 Different Approaches
String Building Examples
String Concatenation
String Interpolation
StringBuilder
String.Format
public string BuildMessage ( string name , int count )
{
string message = "Hello, " + name + "! " ;
message = message + "You have " + count + " messages." ;
return message ;
}
🔐 Access Modifiers
Public Class
Internal Class
Protected Members
Private Implementation
public class PublicClass
{
public string PublicProperty { get ; set ; }
public void PublicMethod ()
{
// Accessible from anywhere
}
}
🚀 LINQ Variations
Query Syntax
Method Syntax
Mixed Syntax
var results = from item in collection
where item . IsActive
orderby item . Name
select new
{
item . Id ,
item . Name
};
🔧 Constructor Patterns
Default Constructor
Parameterized Constructor
Constructor Chaining
Primary Constructor (C# 12)
public class Person
{
public string Name { get ; set ; }
public int Age { get ; set ; }
public Person ()
{
Name = "Unknown" ;
Age = 0 ;
}
}
📝 Exception Handling
Try-Catch
Try-Catch-Finally
Exception Filters
Global Exception Handler
public string ReadFile ( string path )
{
try
{
return File . ReadAllText ( path );
}
catch ( FileNotFoundException )
{
return "File not found" ;
}
catch ( UnauthorizedAccessException )
{
return "Access denied" ;
}
}
💡 Collection Initializers
List Initialization
Dictionary Initialization
Complex Object Initialization
// Traditional
var list = new List < string >();
list . Add ( "Apple" );
list . Add ( "Banana" );
list . Add ( "Cherry" );
// Collection initializer
var list = new List < string >
{
"Apple" ,
"Banana" ,
"Cherry"
};
Pro Tip : Use CodeGroups when you want to show multiple valid approaches to solve the same problem, letting developers choose the one that best fits their needs.
🎯 When to Use CodeGroups
Method Overloads
Show different parameter combinations for the same method
Platform Variations
Display code for different .NET versions or platforms
Alternative Implementations
Present multiple ways to achieve the same result
Progressive Examples
Show basic to advanced usage patterns
CodeGroups are automatically generated by the MintlifyRenderer when it detects method overloads or multiple examples in your documentation.