Skip to content

Instantly share code, notes, and snippets.

@BrodyB
Created November 13, 2024 00:40
Show Gist options
  • Save BrodyB/4337d7ed452c0bfdf07e91ecc23b3151 to your computer and use it in GitHub Desktop.
Save BrodyB/4337d7ed452c0bfdf07e91ecc23b3151 to your computer and use it in GitHub Desktop.
C# Cheat Sheat
// Enumerators are auto-assigned values
// unless you specify them. Here "Agender" is -1,
// and then Woman is 0, Man is 1, etc.
public enum Gender
{
Agender = -1,
Woman,
Man,
Neutral,
DualGender,
TwoSpirit,
Genderflux
}
// A class is the definition of an object, including
// its variables (class members) and functions (which
// are called methods when they're a part of classes)
public class Person
{
// Class Members
public string name; // PUBLIC means it can be accessed by other classes (i.e. Shopkeeper.name)
public float speed = 6.0f;
protected bool alive = true; // PROTECTED means private, but child classes inherit
protected Gender gender;
bool isUninteresting = true; // If an access level isn't defined, it's 'private'.
// PRIVATE means child classes don't get this.
// Properties are really cool. They're called and used
// like variables, but you can define its getter and setter
// as functions. So if someone sets the value of a property,
// you can make sure other things are changed in response.
public string Name
{
get { return name; }
set {
if (value == "")
return;
name = value;
}
}
// Properties can also be regular variable values, but
// you can have getter and setter have different access
// levels. Here, other classes can get this person's age,
// but not set it!
public int Age { get; private set; }
// C# has a lot of shorthand forms you can learn later
// that abbreviates the normal way you write things.
// They're totally optional, but dang they can be
// really nice! This defines a property called "Gender"
// that is only a getter, returning the member "gender"
public Gender Gender => gender;
// "virtual" means it can be overridden by a child class
public virtual void Scream ()
{
// A method that causes the person to remember
// that one time they accidentally spat in their
// biggest crush's fountain drink by accident
}
}
// A Coward is a type of Person. They inherit all the members,
// properties, and methods of Person, and can add their own
// that only Cowards have.
public class Coward : Person
{
private float SanityPercent = 1.0f;
// This method is set to return a boolean value, and therefore
// must always return a boolean value.
public bool KeepComposure()
{
// 'var' create a variable and infers what type it is
var succeeds = Random.float(-0.5, 0.5);
// Only Cowards can try to keep their composure (and may not)
return succeeds <= 0.33f;
}
public override void Scream()
{
base.Scream();
// Extra things happen when a Coward screams!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment