Skip to content

Instantly share code, notes, and snippets.

@restush
Last active October 23, 2023 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save restush/22b1203d47c3eb772bf0362169615615 to your computer and use it in GitHub Desktop.
Save restush/22b1203d47c3eb772bf0362169615615 to your computer and use it in GitHub Desktop.
A correct way to write C# aka order convention of writing a C# class.
public class MyClass
{
// Nested classes, structs, enums
public class PublicNestedClass { }
public struct PublicNestedStruct { }
public enum PublicNestedEnum { A, B, C }
private class PrivateNestedClass { }
private struct PrivateNestedStruct { }
private enum PrivateNestedEnum { A, B, C }
// Public static fields
public static int PublicStaticField;
// Protected static fields
protected static int ProtectedStaticField;
// Private static fields
private static int PrivateStaticField;
// Public properties abstract
public abstract int PublicPropertyAbstract { get; set; }
// Public properties virtual
public virtual int PublicPropertyVirtual { get; set; }
// Public properties override
public override int PublicPropertyVirtual { get; set; }
// Public properties
public int PublicProperty { get; set; }
// Protected properties
protected int ProtectedProperty { get; set; }
// Private properties
private int PrivateProperty { get; set; }
// Public instance const fields
public const int PublicInstanceConstantField = 2023;
// Public instance fields
public int PublicInstanceField; // This should be illegal to write. Use private field instead.
// Protected instance fields
protected int ProtectedInstanceField;
// Private instance const fields
private const int _privateInstanceConstantField = 2023;
// Private instance readonly fields
private readonly int _privateInstanceReadOnlyField;
// Private instance fields
private int _privateInstanceField;
// Public methods
public void PublicMethod() { }
// Protected methods
protected void ProtectedMethod() { }
// Private methods
private void PrivateMethod() { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment