Let's remember C# fundamentals
- Learn about C# without too much theory
- C# language features
- Design patterns
- Writing tests
- Learn about Visual Studio in a practical example
- Code navigation
- Building
- Debugging
- Unit testing
- Create Console App in Visual Studio
- Create Console App in command line
- Windows
- Linux
- Constants
- Working with
Console
- Functional programming with delegates
- See also functional programming sample
public const int BOARD_WIDTH = 25;
public const int BOARD_HEIGHT = 20;
- What does
const
mean? - Which data types can be
const
? - Learn more
Console.Clear();
Console.CursorVisible = false;
...
if (Console.KeyAvailable)
{
var pressedKey = Console.ReadKey();
if ((pressedKey.Modifiers & ConsoleModifiers.Control) != 0)...
}
...
var oldTop = Console.CursorTop;
Console.SetCursorPosition(BORDER_LEFT, BORDER_TOP);
Console.BackgroundColor = BORDER_COLOR;
Console.Write("GAME OVER");
...
private static void RestoreOriginalState(Action drawingFunc)
{
// Do some preparation
drawingFunc();
// Do some cleanup
}
...
RestoreOriginalState(() =>
{
Console.SetCursorPosition(BORDER_LEFT, BORDER_TOP);
Console.BackgroundColor = BORDER_COLOR;
Console.Write(' ');
});
- What does 'Action' mean?
- Discuss 'Func', too.
- See also functional programming sample
- C# XML documentation
- Immutable classes
- Classes vs. structures (aka reference vs. value types)
- See also Memory Consumption Example
- Disassembling (e.g. dnSpy)
/// <summary>
/// Represents a single piece in a tetris game
/// </summary>
public class Piece
{
/// <summary>
/// Initializes a new instance of the <see cref="Piece"/> class
/// </summary>
/// <param name="color">Color of the piece</param>
/// <param name="pattern">Pattern of the piece (see remarks for details)</param>
/// <remarks>
/// An array element set to <c>true</c> in parameter <paramref name="pattern"/> represents
/// a colored pixel. <c>false</c> represents an empty pixel.
/// </remarks>
public Piece(ConsoleColor color, bool[,] pattern) ...
}
public static class Pieces
{
...
}
- What is a
static class
?
private static readonly bool[,] I = { { true, true, true, true } };
private static readonly bool[,] J = { { true, true, true }, { false, false, true } };
- What is the difference between multidimensional array and jagged arrays?
- What does
readonly
mean? - Which data types can be
const
and which can bereadonly
?
private readonly bool[,] content;
...
public bool this[int row, int col]
{
set => content[row, col] = value;
get => content[row, col];
}
- Auto-implemented properties
- Optional arguments
- Performance discussions
- Custom exceptions and
Try...
pattern - Enumerations
- Delegates
- See also Linq exercise
public int CurrentRow { get; private set; } = 0;
public int CurrentCol { get; private set; } = 0;
public Piece CurrentPiece { get; private set; } = null;
public Board(IBoardContent content, RandomPieceGenerator pieceGenerator = null)
{
...
}
public class BoardException : Exception
{
public BoardException() { }
public BoardException(string message) : base(message) { }
public BoardException(string message, Exception innerException) : base(message, innerException) { }
}
public bool TryMergingPatternIntoBoardContent(int targetRow, int targetCol, bool[,] pattern)
{
if (/* Check */) {
return false; // Indicate error
}
// Do something
return true; // Indicate success
}
public void MergePatternIntoBoardContent(int targetRow, int targetCol, bool[,] pattern)
{
if (!TryMergingPatternIntoBoardContent(targetRow, targetCol, pattern)) {
throw new BoardException();
}
}
public enum Direction : int
{
Down = 0,
Left = -1,
Right = 1
}
public delegate Piece RandomPieceGenerator();
...
public static readonly RandomPieceGenerator SinglePixelGenerator =
() => new Piece(ConsoleColor.White, PiecesMockup.SinglePixel);
BoardContentIteratorExtension.cs
- Generic types
- Enumerables
- Linq basics
- Extension methods
- Tuples
- Visual Studio unit testing
- Mock objects
- Exercises