Skip to content

Instantly share code, notes, and snippets.

@CallumCarmicheal
Last active August 12, 2016 20:49
Show Gist options
  • Save CallumCarmicheal/1a864cfe285bd90490d233e82d322f70 to your computer and use it in GitHub Desktop.
Save CallumCarmicheal/1a864cfe285bd90490d233e82d322f70 to your computer and use it in GitHub Desktop.
A rundown of C#'s Syntax

PLEASE NOTE THIS MAY NOT BE 100% ACCURATE BUT IM SURE YOU WILL GET THE IDEA!

Note 1: When creating a property, class etc... You are not required to use the private modifier because the compiler will assume its private - it can be seen as bad pratice by few or not... its usually a personal preference!

Note 2: VARIABLES CREATED FROM WITHIN A METHOD OR FUNCTION (VOID ETC) CAN ONLY BE ACCESSED FROM WITHING THAT FUNCTION!

Note 3: A class has can have multiple objects and variables that can be accessed via the object's instance but not directly from the class like var object = new Class(); object.Var , not the global class: Class.Var

Note 4: While structing inside the c# language it will usually follow something like this:

then either Brackets for a Function/Method and then braces or just braces for a namespace, class, interface, enum, struct

There are advanced cases such as ITokens, a example of this is List<SomeClass> list = new List<SomeClass>(); WATCH OUT FOR THOSE!

#Classes

// Example
class SomeClass {
	// Methods, Variables etc
}

// This class can ONLY contain STATIC methods/functions/variables
static class Name {}

// This class can ONLY contain PUBLIC methods/functions/variables
public class Name {}

// Same idea, it can ONLY have PUBLIC and STATIC items
public static class Name {}

// This class can only be accessed from within the class
// it was made from, IT CANNOT BE USED AS A RETURN TYPE FOR A 
// FUNCTION
private class Name {} /* OR */ class Name {}

Variables

// This can only be accessed from within the class it was made in
string Variable = "";

// This can only be accessed from within the class it was made in 
// but if that class is being created multiple times as a object, this
// will be synced across the classes as a common variable that does not 
// change,
//
// THIS CAN CAUSE MULTI-THREADING ISSUES, IF AT ALL POSSIBLE
// STAY AWAY FROM THIS, EXAMPLE CASES COULD BE:
//
// In the Program.cs FILE, YOU WILL BE CREATING A FORM AND YOU WANT TO
// KEEP TRACK OF ITS INSTANCE, THEN YOU WILL STORE IT AS A STATIC VARIABLE
static string Variable = ""

// This can be accessed from the Class's object instance but not the global class itself!
public string Variable = ""

// Same as the private static variable but this can be accessed from
// anywhere in the program, by calling its STATIC/Global class 
// NOT THE OBJECT's INSTANCE!
//
// THIS CAN CAUSE MULTI-THREADING ISSUES, IF AT ALL POSSIBLE
// STAY AWAY FROM THIS, EXAMPLE CASES COULD BE:
//
// In the Program.cs FILE, YOU WILL BE CREATING A FORM AND YOU WANT TO
// KEEP TRACK OF ITS INSTANCE, THEN YOU WILL STORE IT AS A STATIC VARIABLE
public static string Variable {}

Properties

// A property is a calculated variable
//
// What does that mean?
//    Well its just as it sounds, it will be accessed just like a variable
//    So object.Var but instead it is actually a function, this is used in cases
//    Where the class will need to make sure a variable is not changed.
//    
//    TL;DR  Its a function that is accessed just like a variable.

// Example 1:
//   Making a string readonly but allowing it to be edited from within the 
//   class.  READONLY will create the variable at runtime (app start) but disallow
//   any changes after the variable has first been given a value!
private string _somethingReadOnly; 

public string SomeProperty {
	get {
		// This will return the string but 
		// disallow anything to change the string
		return _somethingReadOnly;
	}
}

// Example 2:
//   Some bizar calulation
// 
public int Einstein {
	get {
		// E=MC2
		var E = MC^2;
		return E;
	}
}

// Example 3: 
//   Input validation
private int _SomeValueThatHasToBeValidated;

public int SetValue {
	set { 
		// When setting a property
		// the value that has been passed 
		// will be under the keyname "value"
		
		// Check for bad data
		// Min = 1
		// Max = 9
		if(value <= 10)
			return; // Exit the function/method instead of contining to next line
		
		if(value >= 0) 
			return;
		
		// Its validated
		_SomeValueThatHasToBeValidated = value;
	}
	get { return _SomeValueThatHasToBeValidated; }
}

Functions

Ill write more later but for now the rundown.

A function is the same as void, but instead you can return data

Example:

public int Add(int x, int y) {
	return x + y;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment