Skip to content

Instantly share code, notes, and snippets.

@melsov
Created September 21, 2018 14:32
Show Gist options
  • Save melsov/b0c19dc9da1d671d881c000045e79cb2 to your computer and use it in GitHub Desktop.
Save melsov/b0c19dc9da1d671d881c000045e79cb2 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEditor;
namespace Fake.ShowCSharpThings
{
public class DemoClass : MonoBehaviour // inheriting monobehaviour so that we can add a menu item
{
//
// Properties
//
// Instead of getter and setter methods
private int _something;
public int GetSomething()
{
return _something;
}
public void SetSomething(int s)
{
_something = s;
}
// you can use a property
public int something {
get {
return _something;
}
set {
_something = value;
}
}
//
// Now outsiders can use:
// int s = demoClass.something;
//
// NOTE: 'value' is a keyword.
// It will hold the value from the right side of an assignment:
// something = 5; <-- 'value' will be 5
// you can also do this
public float usefulNumber {
get {
// multiple lines are fine
float result = (float) Math.Sqrt(_something);
return result;
}
// no set. usefulNumber is read-only
}
// you can also write this:
public int pointless {
get;
set;
}
// As far as I know, this is exactly the same as just owning a public int pointless.
// But...
// if you want let others get a value
// but want to prevent them from setting it
public int onlyICanSet {
get;
private set;
}
//
// Operator overloading
//
public static DemoClass operator+(DemoClass a, DemoClass b)
{
DemoClass c = new DemoClass();
c._something = a._something + b._something;
return c;
}
// also operator-,*,/,%,etc.
//
// now people can write:
// DemoClass d = new DemoClass();
// DemoClass e = new DemoClass();
// DemoClass f = d + e;
//
// This is REALLY useful for Point3 type classes
//
// Implicit casting
//
// You want demo class to implicitly cast to an int:
public static implicit operator int(DemoClass dc)
{
return dc._something;
}
// Be a little bit careful with this one.
// You'll sometimes want to force yourself to remember that
// conversions are taking place.
// anyway, now you can:
// int t = mySomethingObject;
//
// Structs
//
// Structs in C# are a lot like classes
// except structs are value types.
// Classes are reference types.
// This is a big deal!
// It means that assigning a variable to hold
// a struct value creates a copy of that value.
// Changes to the assigned variable wil not
// change the original
// For example:
public struct DemoStruct
{
public int a;
public int structsCanHaveMethodsToo()
{
return a + 5;
}
}
public class DemoImAClass
{
public int a;
}
public void showTheDifference()
{
DemoImAClass c = new DemoImAClass();
c.a = 4;
DemoImAClass c1 = c;
c1.a++;
Debug.Log(c.a); // this is now 5
DemoStruct d = new DemoStruct();
d.a = 4;
DemoStruct d1 = d;
d1.a++;
Debug.Log(d.a); // this is still 4
}
// structs cannot be set to null
// they are just like ints, chars or any other value type in this respect.
// it's a good idea to prefer using structs instead of classes for data container
// types:
public struct Point3i
{
public int x, y, z;
}
// Since, accidentally changing original data when you only meant to change
// the data in (what you thought was) a copy is very easy to do.
//
// var
//
public void youCanUseVar()
{
// instead of:
DemoClass demoClass = new DemoClass();
// you can write:
var altDemoClass = new DemoClass();
// mostly, this just saves you some typing.
// it can save a lot of typing if you
// decide that you need to chang the type of a variable
var myAwesomeLookup = new Dictionary<Vector3Int, string>(); // new HashSet<string>(); // whoops, I need a Dictionary.
// In my opinion, you usually shouldn't use var for primitive types
// because it takes away clarity
int valueFromSomewhere = 5;
//
// ...time passes...
//
var mehDontDoThis = valueFromSomewhere; // wait, what type is this again?
// Do go ahead and use var if it will save you from typing a type name twice on the same line (my opinion)
}
[MenuItem("Demo/Try")]
public static void TryThingsOut()
{
DemoClass d = new DemoClass();
d.showTheDifference();
}
}
//
// Extension methods
//
// For when you want to add methods to a class you didn't write
public static class Vector3Extensions
{
public static Vector3 mult(this Vector3 v, Vector3 other)
{
return Vector3.Scale(v, other);
}
// Vector3 result = v.mult(otherV);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment