Hello World
Console.WriteLine("Hello, world!");
Run the project:
dotnet run
This prints a line to standard output. In a small C# app, Console.WriteLine is the usual starting point.
Variables
string name = "Dan";
int count = 1;
bool active = true;
var language = "C#";
This shows common C# types plus var, which lets the compiler infer the type from the value on the right.
Methods
static string Greet(string name)
{
return $"Hello, {name}!";
}
This is a basic C# method: it accepts an argument, builds a string, and returns the result.
C# Add
static int Add(int param1, int param2)
{
return param1 + param2;
}
This just adds the two input numbers with the language’s normal arithmetic and returns the sum.
C# Add Border
static string[] AddBorder(string[] picture)
{
var rows = new List<string> { new string('*', picture[0].Length) };
rows.AddRange(picture);
for (int i = 0; i < rows.Count; i++)
{
rows[i] = $"*{rows[i]}*";
}
rows.Add(new string('*', rows[0].Length));
return rows.ToArray();
}
This builds a new grid with a * border around every side. It adds a full top and bottom row, then wraps each existing row from left and right.
C# Adjacent Elements Product
static long AdjacentElementsProduct(int[] inputArray)
{
long max = long.MinValue;
for (int i = 0; i < inputArray.Length - 1; i++)
{
max = Math.Max(max, (long)inputArray[i] * inputArray[i + 1]);
}
return max;
}
This walks through neighboring values, multiplies each pair, and keeps the biggest product it finds.
C# Almost Magic Square
static int[] AlmostMagicSquare(int[] a)
{
var rowSum = new int[3];
var colSum = new int[3];
var maxSum = 0;
var grid = new int[3][];
for (int i = 0; i < 3; i++)
{
grid[i] = new[] { a[i * 3], a[i * 3 + 1], a[i * 3 + 2] };
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
rowSum[i] += grid[i][j];
colSum[i] += grid[j][i];
}
}
for (int k = 0; k < 3; k++)
{
maxSum = Math.Max(maxSum, rowSum[k]);
maxSum = Math.Max(maxSum, colSum[k]);
}
for (int i = 0, j = 0; i < 3 && j < 3;)
{
var diff = Math.Min(maxSum - rowSum[i], maxSum - colSum[j]);
grid[i][j] += diff;
rowSum[i] += diff;
colSum[j] += diff;
if (rowSum[i] == maxSum)
{
i++;
}
if (colSum[j] == maxSum)
{
j++;
}
}
var result = new int[9];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
result[i * 3 + j] = grid[i][j];
}
}
return result;
}
This adjusts the matrix toward a matching target sum so the rows and columns line up more like a magic square.
C# Are Equally Strong
static bool AreEquallyStrong(int yourLeft, int yourRight, int friendsLeft, int friendsRight)
{
return Math.Max(yourRight, yourLeft) == Math.Max(friendsLeft, friendsRight)
&& Math.Min(yourLeft, yourRight) == Math.Min(friendsRight, friendsLeft);
}
This compares each person’s strongest and weakest arm. If both pairs match, the result is true.
C# Array Change
static long ArrayChange(int[] a)
{
var arr = new long[a.Length];
Array.Copy(a, arr, a.Length);
long min = 0;
for (int k = 0; k < arr.Length - 1; k++)
{
if (arr[k] >= arr[k + 1])
{
var dif = arr[k] - arr[k + 1] + 1;
arr[k + 1] += dif;
min += dif;
}
}
return min;
}
This moves left to right and bumps values only when needed so the array becomes strictly increasing.
C# Array Maximal Adjacement Difference
static long ArrayMaximalAdjacentDifference(int[] a)
{
long dif = 0;
for (int i = 1; i < a.Length - 1; i++)
{
dif = Math.Max(dif, Math.Max(Math.Abs((long)a[i] - a[i - 1]), Math.Abs((long)a[i] - a[i + 1])));
}
return dif;
}
This checks the gap between each pair of neighbors and returns the largest difference.
C# Binary Gap
static int BinaryGap(int n)
{
var binary = Convert.ToString(n, 2).Trim('0');
var gap = 0;
foreach (var zero in binary.Split('1'))
{
gap = Math.Max(gap, zero.Length);
}
return gap;
}
This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.
C# Bracket
static int Bracket(string s)
{
var stack = new Stack<char>();
foreach (var v in s)
{
switch (v)
{
case ')':
if (stack.Count == 0 || stack.Pop() != '(')
{
return 0;
}
break;
case ']':
if (stack.Count == 0 || stack.Pop() != '[')
{
return 0;
}
break;
case '}':
if (stack.Count == 0 || stack.Pop() != '{')
{
return 0;
}
break;
default:
stack.Push(v);
break;
}
}
return stack.Count == 0 ? 1 : 0;
}
This uses a simple stack approach: open brackets go in, matching closing brackets pop them out.