Skip to content

Instantly share code, notes, and snippets.

@Lamparter
Created June 1, 2025 07:20
Show Gist options
  • Select an option

  • Save Lamparter/512ed5f2bdd4174376eb7fbe4460c2b2 to your computer and use it in GitHub Desktop.

Select an option

Save Lamparter/512ed5f2bdd4174376eb7fbe4460c2b2 to your computer and use it in GitHub Desktop.
Files code style class structure example
using System;
using System.Collections.Generic;
using System.Windows.Input;
using System.Diagnostics;
public class ExampleClass : IDisposable
{
// ๐Ÿ”น Static fields
public static int InstanceCount = 0;
// ๐Ÿ”น Static properties
public static string ClassName => nameof(ExampleClass);
// ๐Ÿ”น Readonly fields
private readonly string _id;
// ๐Ÿ”น Fields
private int _counter;
private bool _disposed;
// ๐Ÿ”น Service property
public IServiceProvider ServiceProvider { get; }
// ๐Ÿ”น Properties
public int Counter
{
get => _counter;
set => _counter = value;
}
// ๐Ÿ”น UI properties
public string DisplayText { get; set; }
// ๐Ÿ”น ICommand properties
public ICommand ClickCommand { get; }
// ๐Ÿ”น Constructor
public ExampleClass(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
_id = Guid.NewGuid().ToString();
ClickCommand = new RelayCommand(ExecuteCommand);
InstanceCount++;
}
// ๐Ÿ”น Command implementation functions
private void ExecuteCommand()
{
Console.WriteLine("Command executed!");
}
// ๐Ÿ”น Public methods
public void PrintDetails()
{
Console.WriteLine($"ID: {_id}, Counter: {_counter}");
}
// ๐Ÿ”น Protected methods
protected virtual void OnUpdated()
{
Console.WriteLine("Updated!");
}
// ๐Ÿ”น Private methods
private void ResetCounter()
{
_counter = 0;
}
// ๐Ÿ”น Static methods
public static void ShowInstanceCount()
{
Console.WriteLine($"Total instances: {InstanceCount}");
}
// ๐Ÿ”น Abstract methods (if applicable)
public abstract class BaseExample
{
public abstract void AbstractMethod();
}
// ๐Ÿ”น Operators
public static ExampleClass operator +(ExampleClass a, ExampleClass b)
{
return new ExampleClass(a.ServiceProvider) { Counter = a.Counter + b.Counter };
}
// ๐Ÿ”น Assertions
private void AssertNotDisposed()
{
Debug.Assert(!_disposed, "Object already disposed!");
}
// ๐Ÿ”น IDisposable.Dispose()
public void Dispose()
{
_disposed = true;
GC.SuppressFinalize(this);
}
}
// Example ICommand implementation
public class RelayCommand : ICommand
{
private readonly Action _execute;
public RelayCommand(Action execute) => _execute = execute;
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter) => true;
public void Execute(object parameter) => _execute();
}
@Lamparter

Copy link
Copy Markdown
Author

Oops I just realised this document uses spaces for indentation rather than tabs lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment