Skip to main content

Code Groups for C# Documentation

CodeGroups are perfect for showing different implementations, overloads, or usage patterns of the same API.

📋 Method Overloads

public void Process()
{
    // Process with default settings
    ProcessInternal(DefaultOptions);
}

🔄 Synchronous vs Asynchronous

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

public string BuildMessage(string name, int count)
{
    string message = "Hello, " + name + "! ";
    message = message + "You have " + count + " messages.";
    return message;
}

🔐 Access Modifiers

public class PublicClass
{
    public string PublicProperty { get; set; }
    
    public void PublicMethod()
    {
        // Accessible from anywhere
    }
}

🚀 LINQ Variations

var results = from item in collection
              where item.IsActive
              orderby item.Name
              select new
              {
                  item.Id,
                  item.Name
              };

🔧 Constructor Patterns

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    
    public Person()
    {
        Name = "Unknown";
        Age = 0;
    }
}

📝 Exception Handling

public string ReadFile(string path)
{
    try
    {
        return File.ReadAllText(path);
    }
    catch (FileNotFoundException)
    {
        return "File not found";
    }
    catch (UnauthorizedAccessException)
    {
        return "Access denied";
    }
}

💡 Collection Initializers

// 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

1

Method Overloads

Show different parameter combinations for the same method
2

Platform Variations

Display code for different .NET versions or platforms
3

Alternative Implementations

Present multiple ways to achieve the same result
4

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.