Skip to content

Latest commit

 

History

History
330 lines (237 loc) · 9.44 KB

csharp-recap.md

File metadata and controls

330 lines (237 loc) · 9.44 KB

C# Recap

Let's remember C# fundamentals

Learn by Example

Tetris Sample

Sample Code

Learn by Example (cont.)

  • 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 .NET Console App

  • Create Console App in Visual Studio
  • Create Console App in command line
    • Windows
    • Linux

Draw Tetris Border and Usage

Program.cs

Constants

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

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");
...

Functional Programming Basics

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(' ');
});

Tetris Piece

Piece.cs

C# XML Code Documentation

/// <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) ...
}

Pieces.cs

Static Classes

public static class Pieces
{
  ...
}
  • What is a static class?

Multidimensional Arrays, readonly

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 be readonly?

Board Content

BoardContent.cs

Indexer and Expression-bodied Members

private readonly bool[,] content;
...
public bool this[int row, int col]
{
  set => content[row, col] = value;
  get => content[row, col];
}

Board

Board.cs BoardException

Auto-implemented Properties

public int CurrentRow { get; private set; } = 0;
public int CurrentCol { get; private set; } = 0;
public Piece CurrentPiece { get; private set; } = null;

Optional Arguments

public Board(IBoardContent content, RandomPieceGenerator pieceGenerator = null)
{
  ...
}

Custom Exceptions

public class BoardException : Exception
{
  public BoardException() { }

  public BoardException(string message) : base(message) { }

  public BoardException(string message, Exception innerException) : base(message, innerException) { }
}

Try... Pattern

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();
  }
}

Enumerations

public enum Direction : int
{
  Down = 0,
  Left = -1,
  Right = 1
}

Delegates

public delegate Piece RandomPieceGenerator();
...
public static readonly RandomPieceGenerator SinglePixelGenerator =
  () => new Piece(ConsoleColor.White, PiecesMockup.SinglePixel);

Board Content Iterator

BoardContentIteratorExtension.cs

Unit Testing

Tetris.Tests Test example

Further Readings and Exercises