-
-
Save Lamparter/512ed5f2bdd4174376eb7fbe4460c2b2 to your computer and use it in GitHub Desktop.
Files code style class structure example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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(); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oops I just realised this document uses spaces for indentation rather than tabs lol