public ulong[][] pieces;
public ulong[] occupancy;
public ulong occupancyAll;
public ulong enPassant;
public ulong castling;
public int colorToMove;
public int movesCount;
public int halfMoveClock;
public int nullMoves;
public int[] PSTValuesOpening;
public int[] PSTValuesEnding;
public int currentGamePhase;
public List<int> capturedPieces;
public List<int> promotedPieces;
public List<ulong> castlings;
public List<ulong> zobristKeys;
public List<ulong> enPassants;
public List<int> halfMoveClockList;
public ulong zobristKey;
public Move[] possibleMoves;
public int movesAdded;
public string stalemateReason;
public int[] kingPositions;
public BoardState()
{
// Init bitboards
pieces = new ulong[2][];
pieces[Color.White] = new ulong[6];
pieces[Color.Black] = new ulong[6];
occupancy = new ulong[2];
// Init lists
PSTValuesOpening = new int[2];
PSTValuesEnding = new int[2];
capturedPieces = new List<int>();
promotedPieces = new List<int>();
castlings = new List<ulong>();
zobristKeys = new List<ulong>();
enPassants = new List<ulong>();
halfMoveClockList = new List<int>();
// Init other variables
currentGamePhase = 0;
kingPositions = new int[2];
}
// -----------------------------------------------------------------------------------
// ###################################################################################
// GENERATE MOVES
// ###################################################################################
// -----------------------------------------------------------------------------------
public Move[] GetAllMoves()
{
// Clear list of moves
possibleMoves = new Move[GameConstants.maxMovesInAPosition];
movesAdded = 0;
GetQuietMoves(false);
GetCaptureMoves(false);
return possibleMoves;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void AddAllTargetSquares(byte fromSquare, ulong targets, byte piece, MoveFlags flag)
{
for (; targets != 0; targets = GeneralBitOperations.PopLSB(targets))
{
AddMoveToPossibleMoves(fromSquare, (byte)GeneralBitOperations.GetLSBIndex(targets), piece, (byte)flag);
}
}
public Move[] GetQuietMoves(bool justQuiets)
{
// Clear list of moves if we should just get the quiet moves
if (justQuiets)
{
possibleMoves = new Move[GameConstants.maxMovesInAPosition];
movesAdded = 0;
}
// ##############################################################
// -------------- Go through all pieces ------------------------
// ##############################################################
byte fromSquare;
// Pawns
int direction = colorToMove == Color.White ? -8 : 8;
for (ulong pieceBB = pieces[colorToMove][Piece.Pawn]; pieceBB != 0; pieceBB = GeneralBitOperations.PopLSB(pieceBB))
{
fromSquare = (byte)GeneralBitOperations.GetLSBIndex(pieceBB);
// Check that square is empty in front of pawn
if ((Pawns.QuietMasksOneStep[colorToMove][fromSquare] & occupancyAll) == 0)
{
// One step moves
// Check if from square is on a promoting row
if ((GameConstants.fromPromotionRows[colorToMove] & (1ul << fromSquare)) != 0)
{
AddMoveToPossibleMoves(fromSquare, (byte)(fromSquare + direction), Piece.Pawn, (byte)MoveFlags.PromotionKnight);
AddMoveToPossibleMoves(fromSquare, (byte)(fromSquare + direction), Piece.Pawn, (byte)MoveFlags.PromotionBishop);
AddMoveToPossibleMoves(fromSquare, (byte)(fromSquare + direction), Piece.Pawn, (byte)MoveFlags.PromotionRook);
AddMoveToPossibleMoves(fromSquare, (byte)(fromSquare + direction), Piece.Pawn, (byte)MoveFlags.PromotionQueen);
}
// Else a normal one step move
else
{
AddMoveToPossibleMoves(fromSquare, (byte)(fromSquare + direction), Piece.Pawn, (byte)MoveFlags.Quiet);
}
// Two step moves, if pawn is on start row and no pawns on end square
if ((GameConstants.pawnStartRow[colorToMove] & (1ul << fromSquare)) != 0)
{
if ((Pawns.QuietMasksTwoStep[colorToMove][fromSquare] & occupancyAll) == 0)
{
AddMoveToPossibleMoves(fromSquare, (byte)(fromSquare + 2 * direction), Piece.Pawn, (byte)MoveFlags.DoublePawnPush);
}
}
}
}
// Knights
for (ulong pieceBB = pieces[colorToMove][Piece.Knight]; pieceBB != 0; pieceBB = GeneralBitOperations.PopLSB(pieceBB))
{
fromSquare = (byte)GeneralBitOperations.GetLSBIndex(pieceBB);
AddAllTargetSquares(fromSquare, Knights.AttackMasks[fromSquare] & ~occupancyAll, Piece.Knight, MoveFlags.Quiet);
}
// Bishops
for (ulong pieceBB = pieces[colorToMove][Piece.Bishop]; pieceBB != 0; pieceBB = GeneralBitOperations.PopLSB(pieceBB))
{
fromSquare = (byte)GeneralBitOperations.GetLSBIndex(pieceBB);
AddAllTargetSquares(fromSquare, Bishops.GetAttacks(fromSquare, occupancyAll) & ~occupancyAll, Piece.Bishop, MoveFlags.Quiet);
}
// Rooks
for (ulong pieceBB = pieces[colorToMove][Piece.Rook]; pieceBB != 0; pieceBB = GeneralBitOperations.PopLSB(pieceBB))
{
fromSquare = (byte)GeneralBitOperations.GetLSBIndex(pieceBB);
AddAllTargetSquares(fromSquare, Rooks.GetAttacks(fromSquare, occupancyAll) & ~occupancyAll, Piece.Rook, MoveFlags.Quiet);
}
// Queens
for (ulong pieceBB = pieces[colorToMove][Piece.Queen]; pieceBB != 0; pieceBB = GeneralBitOperations.PopLSB(pieceBB))
{
fromSquare = (byte)GeneralBitOperations.GetLSBIndex(pieceBB);
AddAllTargetSquares(fromSquare, Queens.GetAttacks(fromSquare, occupancyAll) & ~occupancyAll, Piece.Queen, MoveFlags.Quiet);
}
// Kings
fromSquare = (byte)kingPositions[colorToMove];
AddAllTargetSquares(fromSquare, Kings.AttackMasks[fromSquare] & ~occupancyAll, Piece.King, MoveFlags.Quiet);
// Castling
// White side
if (colorToMove == Color.White)
{
// King side
if (Kings.WhiteCanCastleKingSide(this, colorToMove))
{
AddMoveToPossibleMoves(fromSquare, (byte)(fromSquare + 2), Piece.King, (byte)MoveFlags.Castle);
}
// Queen side
if (Kings.WhiteCanCastleQueenSide(this, colorToMove))
{
AddMoveToPossibleMoves(fromSquare, (byte)(fromSquare - 2), Piece.King, (byte)MoveFlags.Castle);
}
}
// Black side
else
{
// King side
if (Kings.BlackCanCastleKingSide(this, colorToMove))
{
AddMoveToPossibleMoves(fromSquare, (byte)(fromSquare + 2), Piece.King, (byte)MoveFlags.Castle);
}
// Queen side
if (Kings.BlackCanCastleQueenSide(this, colorToMove))
{
AddMoveToPossibleMoves(fromSquare, (byte)(fromSquare - 2), Piece.King, (byte)MoveFlags.Castle);
}
}
return possibleMoves;
}
public Move[] GetCaptureMoves(bool justCaptures)
{
// Clear list of moves if we should just get capturing moves
if (justCaptures)
{
possibleMoves = new Move[GameConstants.maxMovesInAPosition];
movesAdded = 0;
}
// ##############################################################
// -------------- Go through all pieces ------------------------
// ##############################################################
byte fromSquare;
// Pawns
for (ulong pieceBB = pieces[colorToMove][Piece.Pawn]; pieceBB != 0; pieceBB = GeneralBitOperations.PopLSB(pieceBB))
{
fromSquare = (byte)GeneralBitOperations.GetLSBIndex(pieceBB);
ulong targets = Pawns.AttackMasks[colorToMove][fromSquare] & occupancy[Color.Invert(colorToMove)];
// Check for promotions
if ((GameConstants.fromPromotionRows[colorToMove] & (1ul << fromSquare)) != 0)
{
AddAllTargetSquares(fromSquare, targets, Piece.Pawn, MoveFlags.PromotionKnightCapture);
AddAllTargetSquares(fromSquare, targets, Piece.Pawn, MoveFlags.PromotionBishopCapture);
AddAllTargetSquares(fromSquare, targets, Piece.Pawn, MoveFlags.PromotionRookCapture);
AddAllTargetSquares(fromSquare, targets, Piece.Pawn, MoveFlags.PromotionQueenCapture);
}
else
{
AddAllTargetSquares(fromSquare, targets, Piece.Pawn, MoveFlags.Capture);
}
// Enpassant
if (enPassant != 0)
{
ulong epAttacks = Pawns.AttackMasks[colorToMove][fromSquare] & enPassant;
if (epAttacks != 0)
{
byte toSquare = (byte)GeneralBitOperations.GetLSBIndex(epAttacks);
AddMoveToPossibleMoves(fromSquare, toSquare, Piece.Pawn, (byte)MoveFlags.EnPassant);
}
}
}
// Knights
for (ulong pieceBB = pieces[colorToMove][Piece.Knight]; pieceBB != 0; pieceBB = GeneralBitOperations.PopLSB(pieceBB))
{
fromSquare = (byte)GeneralBitOperations.GetLSBIndex(pieceBB);
AddAllTargetSquares(fromSquare, Knights.AttackMasks[fromSquare] & occupancy[Color.Invert(colorToMove)], Piece.Knight, MoveFlags.Capture);
}
// Bishops
for (ulong pieceBB = pieces[colorToMove][Piece.Bishop]; pieceBB != 0; pieceBB = GeneralBitOperations.PopLSB(pieceBB))
{
fromSquare = (byte)GeneralBitOperations.GetLSBIndex(pieceBB);
AddAllTargetSquares(fromSquare, Bishops.GetAttacks(fromSquare, occupancyAll) & occupancy[Color.Invert(colorToMove)], Piece.Bishop, MoveFlags.Capture);
}
// Rooks
for (ulong pieceBB = pieces[colorToMove][Piece.Rook]; pieceBB != 0; pieceBB = GeneralBitOperations.PopLSB(pieceBB))
{
fromSquare = (byte)GeneralBitOperations.GetLSBIndex(pieceBB);
AddAllTargetSquares(fromSquare, Rooks.GetAttacks(fromSquare, occupancyAll) & occupancy[Color.Invert(colorToMove)], Piece.Rook, MoveFlags.Capture);
}
// Queens
for (ulong pieceBB = pieces[colorToMove][Piece.Queen]; pieceBB != 0; pieceBB = GeneralBitOperations.PopLSB(pieceBB))
{
fromSquare = (byte)GeneralBitOperations.GetLSBIndex(pieceBB);
AddAllTargetSquares(fromSquare, Queens.GetAttacks(fromSquare, occupancyAll) & occupancy[Color.Invert(colorToMove)], Piece.Queen, MoveFlags.Capture);
}
// Kings
fromSquare = (byte)kingPositions[colorToMove];
AddAllTargetSquares(fromSquare, Kings.AttackMasks[fromSquare] & occupancy[Color.Invert(colorToMove)], Piece.King, MoveFlags.Capture);
return possibleMoves;
}
// -----------------------------------------------------------------------------------
// ###################################################################################
// MAKE AND UNMAKE MOVES
// ###################################################################################
// -----------------------------------------------------------------------------------
public bool MakeMove(Move move)
{
// Increase counters
if (colorToMove == Color.White) movesCount++;
// Add variables that can't be tracked in any other way to lists
castlings.Add(castling);
zobristKeys.Add(zobristKey);
enPassants.Add(enPassant);
halfMoveClockList.Add(halfMoveClock);
// Update king position
if (move.pieceMoved == Piece.King)
{
kingPositions[colorToMove] = move.toSquare;
}
// Increase halfmove clock, resets to 0 further down if applicable
halfMoveClock++;
// Check if enpassant is possible and if so disable it
if (enPassant != 0)
{
int enPassantRank = GeneralBitOperations.BitScan(enPassant) % 8;
zobristKey = Zobrist.ToggleEnPassant(zobristKey, enPassantRank);
enPassant = 0;
}
// ------------------------------------------------------
// Check for different move types
// ------------------------------------------------------
// Quiet moves that are not double pushes
if (MoveFunctions.IsQuiet(move.flag))
{
MovePiece(colorToMove, move.pieceMoved, move.fromSquare, move.toSquare);
zobristKey = Zobrist.MovePiece(zobristKey, colorToMove, move.pieceMoved, move.fromSquare, move.toSquare);
// Reset halfmove clock if pawn move
if (move.pieceMoved == Piece.Pawn) halfMoveClock = 0;
}
// Double pawn pushes
else if (MoveFunctions.IsDoublePawnPush(move.flag))
{
// Reset halfmove clock
halfMoveClock = 0;
// Move piece
MovePiece(colorToMove, move.pieceMoved, move.fromSquare, move.toSquare);
zobristKey = Zobrist.MovePiece(zobristKey, colorToMove, move.pieceMoved, move.fromSquare, move.toSquare);
// Enable enpassant
ulong epSquare = colorToMove == Color.White ? 1ul << move.toSquare + 8 : 1ul << move.toSquare - 8;
enPassant |= epSquare;
zobristKey = Zobrist.ToggleEnPassant(zobristKey, GeneralBitOperations.BitScan(epSquare) % 8);
}
// Enpassant moves
else if (MoveFunctions.IsEnPassant(move.flag))
{
// Find enemy pawn (which is not on the toSquare)
int enemyPieceSquare = colorToMove == Color.White ? move.toSquare + 8 : move.toSquare - 8;
int capturedPiece = Piece.Pawn;
capturedPieces.Add(capturedPiece);
// Move own pawn and remove enemy pawn
MovePiece(colorToMove, move.pieceMoved, move.fromSquare, move.toSquare);
zobristKey = Zobrist.MovePiece(zobristKey, colorToMove, move.pieceMoved, move.fromSquare, move.toSquare);
RemovePiece(Color.Invert(colorToMove), capturedPiece, enemyPieceSquare);
zobristKey = Zobrist.AddOrRemovePiece(zobristKey, Color.Invert(colorToMove), capturedPiece, enemyPieceSquare);
}
// Capture moves
else if (MoveFunctions.IsCapture(move.flag))
{
// Reset halfmove clock
halfMoveClock = 0;
// Remove captured piece from its square and add it to list of captured pieces
int capturedPiece = MoveFunctions.GetCapturedPiece(pieces[Color.Invert(colorToMove)], move.toSquare);
RemovePiece(Color.Invert(colorToMove), capturedPiece, move.toSquare);
zobristKey = Zobrist.AddOrRemovePiece(zobristKey, Color.Invert(colorToMove), capturedPiece, move.toSquare);
capturedPieces.Add(capturedPiece);
// Check for promotion captures and if so move in special way
if (MoveFunctions.IsPromotion(move.flag))
{
// Find the piece that we should promote to
int promotionPiece = GetPromotionPiece(move.flag);
// Remove pawn from square and add the promoted piece instead
RemovePiece(colorToMove, move.pieceMoved, move.fromSquare);
zobristKey = Zobrist.AddOrRemovePiece(zobristKey, colorToMove, move.pieceMoved, move.fromSquare);
AddPiece(colorToMove, promotionPiece, move.toSquare);
zobristKey = Zobrist.AddOrRemovePiece(zobristKey, colorToMove, promotionPiece, move.toSquare);
// Add to list of promoted pieces
promotedPieces.Add(promotionPiece);
}
// If not it is a normal capture
else
{
// Move the piece as normal
MovePiece(colorToMove, move.pieceMoved, move.fromSquare, move.toSquare);
zobristKey = Zobrist.MovePiece(zobristKey, colorToMove, move.pieceMoved, move.fromSquare, move.toSquare);
}
}
// Castling moves
else if (MoveFunctions.IsCastling(move.flag))
{
// Reset halfmove clock
halfMoveClock = 0;
// Get from and to square for the rook
int rookFromSquare = GameConstants.rookCastling[move.toSquare][0];
int rookToSquare = GameConstants.rookCastling[move.toSquare][1];
// Move rook and king to the correct places
MovePiece(colorToMove, Piece.King, move.fromSquare, move.toSquare);
MovePiece(colorToMove, Piece.Rook, rookFromSquare, rookToSquare);
zobristKey = Zobrist.MovePiece(zobristKey, colorToMove, Piece.King, move.fromSquare, move.toSquare);
zobristKey = Zobrist.MovePiece(zobristKey, colorToMove, Piece.Rook, rookFromSquare, rookToSquare);
}
// Normal promotion moves that are not captures
else if (MoveFunctions.IsPromotion(move.flag))
{
// Get what piece was promoted
int promotionPiece = GetPromotionPiece(move.flag);
// Remove pawn from square and add promoted piece on toSquare
RemovePiece(colorToMove, Piece.Pawn, move.fromSquare);
zobristKey = Zobrist.AddOrRemovePiece(zobristKey, colorToMove, move.pieceMoved, move.fromSquare);
AddPiece(colorToMove, promotionPiece, move.toSquare);
zobristKey = Zobrist.AddOrRemovePiece(zobristKey, colorToMove, move.pieceMoved, move.toSquare);
// Add piece to list
promotedPieces.Add(promotionPiece);
}
// If castling rights, check and remove the correct ones
if (castling != 0)
{
ulong updateMask = GameConstants.updateCastlingRights[move.fromSquare] & GameConstants.updateCastlingRights[move.toSquare];
castling &= updateMask;
zobristKey = Zobrist.AddOrRemoveCastlingRights(zobristKey, updateMask);
}
// Check if move is legal (king is not under attack after move is done)
if (!IsSquareAttacked(colorToMove, kingPositions[colorToMove]))
{
// Change turns
colorToMove = Color.Invert(colorToMove);
zobristKey = Zobrist.ChangeSide(zobristKey);
return true;
}
// If not, then unmake the move and return false
else
{
// Change turns
colorToMove = Color.Invert(colorToMove);
zobristKey = Zobrist.ChangeSide(zobristKey);
UnmakeMove(move);
return false;
}
}
public void UnmakeMove(Move move)
{
// Change turn
colorToMove = Color.Invert(colorToMove);
// Update king position
if (move.pieceMoved == Piece.King)
{
kingPositions[colorToMove] = move.fromSquare;
}
// Cache last indices
int lastIndexCaptured = capturedPieces.Count - 1;
int lastIndexPromoted = promotedPieces.Count - 1;
int lastIndexHalfMoveClock = halfMoveClockList.Count - 1;
int lastIndexZobristKeys = zobristKeys.Count - 1;
int lastIndexCastlings = castlings.Count - 1;
int lastIndexEnPassants = enPassants.Count - 1;
// Handle different move types
if (MoveFunctions.IsQuiet(move.flag) || MoveFunctions.IsDoublePawnPush(move.flag))
{
MovePiece(colorToMove, move.pieceMoved, move.toSquare, move.fromSquare);
}
else if (MoveFunctions.IsEnPassant(move.flag))
{
int enemyColor = Color.Invert(colorToMove);
int enemyPieceField = colorToMove == Color.White ? (move.toSquare + 8) : (move.toSquare - 8);
int capturedPiece = capturedPieces[lastIndexCaptured];
MovePiece(colorToMove, Piece.Pawn, move.toSquare, move.fromSquare);
AddPiece(Color.Invert(colorToMove), capturedPiece, enemyPieceField);
// Remove last captured piece directly
capturedPieces.RemoveAt(lastIndexCaptured);
}
else if (MoveFunctions.IsCapture(move.flag))
{
int enemyColor = Color.Invert(colorToMove);
int capturedPiece = capturedPieces[lastIndexCaptured];
// Capturing promotion
if (MoveFunctions.IsPromotion(move.flag))
{
int promotedPiece = promotedPieces[lastIndexPromoted];
promotedPieces.RemoveAt(lastIndexPromoted);
RemovePiece(colorToMove, promotedPiece, move.toSquare);
AddPiece(colorToMove, Piece.Pawn, move.fromSquare);
}
else
{
MovePiece(colorToMove, move.pieceMoved, move.toSquare, move.fromSquare);
}
AddPiece(enemyColor, capturedPiece, move.toSquare);
// Remove last captured piece directly
capturedPieces.RemoveAt(lastIndexCaptured);
}
else if (MoveFunctions.IsCastling(move.flag))
{
// Get from and to square for the rook
int rookFromSquare = GameConstants.rookCastling[move.toSquare][0];
int rookToSquare = GameConstants.rookCastling[move.toSquare][1];
// Move rook and king to the correct places
MovePiece(colorToMove, Piece.King, move.toSquare, move.fromSquare);
MovePiece(colorToMove, Piece.Rook, rookToSquare, rookFromSquare);
}
else if (MoveFunctions.IsPromotion(move.flag))
{
int promotedPiece = promotedPieces[lastIndexPromoted];
promotedPieces.RemoveAt(lastIndexPromoted);
RemovePiece(colorToMove, promotedPiece, move.toSquare);
AddPiece(colorToMove, Piece.Pawn, move.fromSquare);
}
// Update with latest move variables
halfMoveClock = halfMoveClockList[lastIndexHalfMoveClock];
zobristKey = zobristKeys[lastIndexZobristKeys];
castling = castlings[lastIndexCastlings];
enPassant = enPassants[lastIndexEnPassants];
// Remove from lists directly
halfMoveClockList.RemoveAt(lastIndexHalfMoveClock);
zobristKeys.RemoveAt(lastIndexZobristKeys);
castlings.RemoveAt(lastIndexCastlings);
enPassants.RemoveAt(lastIndexEnPassants);
// Decrease moves count if white to move
if (colorToMove == Color.White) movesCount--;
}
public static class GeneralBitOperations
{
private static readonly int[] BitScanValues =
{
0, 1, 48, 2, 57, 49, 28, 3,
61, 58, 50, 42, 38, 29, 17, 4,
62, 55, 59, 36, 53, 51, 43, 22,
45, 39, 33, 30, 24, 18, 12, 5,
63, 47, 56, 27, 60, 41, 37, 16,
54, 35, 52, 21, 44, 32, 23, 11,
46, 26, 40, 15, 34, 20, 31, 10,
25, 14, 19, 9, 13, 8, 7, 6
};
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong SetBit(ulong bitboard, int square)
{
return (bitboard) |= (1ul << square);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong PopBit(ulong bitboard, int square)
{
return GetBit(bitboard, square) != 0 ? (bitboard &= ~(1ul << (square))) : 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong GetBit(ulong bitboard, int square)
{
return (bitboard) & (1ul << square);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong GetLSB(ulong bitboard)
{
return (ulong)((long)bitboard & -(long)bitboard);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetLSBIndex(ulong bitboard)
{
return BitOperations.PopCount((ulong)((long)bitboard & -(long)bitboard) - 1);
// return BitOperations.TrailingZeroCount(bitboard);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong PopLSB(ulong bitboard)
{
return bitboard & (bitboard - 1);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int BitScan(ulong bitboard)
{
return BitScanValues[((ulong)((long)bitboard & -(long)bitboard) * 0x03f79d71b4cb0a89) >> 58];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static List<int> GetOccupiedSquares(ulong bitboard)
{
List<int> squares = new List<int>();
while (bitboard != GameConstants.EmptyBitboard)
{
squares.Add(BitScan(bitboard));
bitboard = PopLSB(bitboard);
}
return squares;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong SetOccupancyBitboards(int index, int bitsInMask, ulong attackMask)
{
ulong occupancy = 0ul;
// Loop over range of bits withing attack mask
for (int count = 0; count < bitsInMask; count++)
{
// Get LSB index of attacks mask
int square = GetLSBIndex(attackMask);
// Pop the LSB in attack mask
attackMask = PopBit(attackMask, square);
// Make sure occupancy is on board and populate occupancy mask
if ((index & (1 << count)) != 0)
{
occupancy |= (1ul << square);
}
}
return occupancy;
}
} Write, Run & Share C# code online using OneCompiler's C# online compiler for free. It's one of the robust, feature-rich online compilers for C# language, running on the latest version 8.0. Getting started with the OneCompiler's C# compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C# and start coding.
OneCompiler's C# online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
using System;
namespace Sample
{
class Test
{
public static void Main(string[] args)
{
string name;
name = Console.ReadLine();
Console.WriteLine("Hello {0} ", name);
}
}
}
C# is a general purpose object-oriented programming language by Microsoft. Though initially it was developed as part of .net but later it was approved by ECMA and ISO standards.
You can use C# to create variety of applications, like web, windows, mobile, console applications and much more using Visual studio.
| Data Type | Description | Range | size |
|---|---|---|---|
| int | To store integers | -2,147,483,648 to 2,147,483,647 | 4 bytes |
| double | to store large floating point numbers with decimals | can store 15 decimal digits | 8 bytes |
| float | to store floating point numbers with decimals | can store upto 7 decimal digits | 4 bytes |
| char | to store single characters | - | 2 bytes |
| string | to stores text | - | 2 bytes per character |
| bool | to stores either true or false | - | 1 bit |
datatype variable-name = value;
When ever you want to perform a set of operations based on a condition or set of few conditions IF-ELSE is used.
if(conditional-expression) {
// code
}
else {
// code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
// code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement) {
// code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.
data-type[] array-name;
Method is a set of statements which gets executed only when they are called. Call the method name in the main function to execute the method.
static void method-name()
{
// code to be executed
}