kota's memex

A class bundles together data (fields) and operations (methods). A class is always a reference type, like a string or array, but unlike an int or a struct:

public class Functions
{
	public static decimal Factorial(int n)
	{
		// Check for invalid input.
		if ((n < 0) || (n > 20))
		{
			return -1;
		}

		// Calculate iteratively.
		decimal tempResult = 1;
		for (int i = 1; i <= n; i++)
		{
			tempResult *= i;
		}
		return tempResult;
	}
}

class MainClass
{
	static int Main(string[] args)
	{
		if (args.Length == 0)
		{
			return 1;
		}

		int num;
		bool test = int.TryParse(args[0], out num);
		if (!test)
		{
			return 1;
		}

		decimal result = Functions.Factorial(num);
		if (result == -1)
		{
			Console.WriteLine("Input must be >= 0 and <= 20.");
		}
		else
		{
			Console.WriteLine($"The factorial of {num} is {result}.");
		}
		return 0;
	}
}

c sharp properties

Properties give you field like access while still protecting data with methods.

c sharp access modifiers

Define where a class or its members can be used.

objects

An object is a reference to an instance of a class. A pointer.

Customer c = new Customer();

c sharp constructors

Different ways to set default field values in an object.

c sharp inheritance

anonymous types

Using object initializer syntax, var, and properties you can create an object that does not have a formal name or definition:

var anon = new { Name = "Steve", Age = 34 };
Console.WriteLine($"{anon.Name} is {anon.Age} years old.");

Both properties are get only. Usually it's better to create a tiny class for this purpose or use a tuple.

abstract class

A class can be declared as abstract. An abstract class contains abstract methods that have a signature definition, but no implementation. They cannot be instantiated. They can only be used through derived classes that implement the abstract methods.

A common example would be, a class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class.

public abstract class A
{
    public abstract void DoWork(int i);
}

Abstract methods have no implementation, so the method definition is followed by a semicolon instead of a normal method block.

public class D
{
    public virtual void DoWork(int i)
    {
        // Original implementation.
    }
}

public abstract class E : D
{
    public abstract override void DoWork(int i);
}

public class F : E
{
    public override void DoWork(int i)
    {
        // New implementation.
    }
}

abstract classes vs interfaces

They're quite similar, but an abstract class can specify default implementations of methods. An abstract class is more often used as a "base class" or a partial implementation. Also interfaces, support multiple inheritance, unlike classes.

sealed class

A sealed class is contrasted with an abstract class by not allowing other classes to derive from it.