Skip to content

Instantly share code, notes, and snippets.

@akrisanov
Last active May 1, 2022 19:25
Show Gist options
  • Save akrisanov/37c0f47a06128f4bdc91eb9931b11fcd to your computer and use it in GitHub Desktop.
Save akrisanov/37c0f47a06128f4bdc91eb9931b11fcd to your computer and use it in GitHub Desktop.
C# 101
using System;
class Program {
static void Main(string[] args) {
var name = "Microsoft";
Console.WriteLine($"Hello, {name}!");
}
}
using System;
class Program {
static void Main(string[] args) {
var name = "Microsoft";
Console.WriteLine($"The name `{name}` has {name.Length} letters.");
}
}
using System;
class Program {
static void Main(string[] args) {
var name = " Microsoft ";
Console.WriteLine($"Hello, {name.TrimStart()}!");
Console.WriteLine($"Hello, {name.TrimEnd()}!");
Console.WriteLine($"Hello, {name.Trim()}!");
}
}
using System;
class Program {
static void Main(string[] args) {
var name = "Microsoft";
Console.WriteLine($"Hello, {name.Replace("Micro", "Macro")}!");
}
}
using System;
class Program {
static void Main(string[] args) {
var name = "Microsoft";
Console.WriteLine(name.Contains("soft"));
Console.WriteLine(name.StartsWith("Micro"));
Console.WriteLine(name.EndsWith("t"));
}
}
using System;
class Program {
static void Main(string[] args) {
// Basic operations
int a = 18;
int b = 6;
int c = a + b;
Console.WriteLine(c);
Console.WriteLine(a + b * c);
Console.WriteLine((a + b) / c);
Console.WriteLine();
// Shorts
Console.WriteLine($"The range of shorts is {short.MinValue} to {short.MaxValue}");
Console.WriteLine();
// Integer range
int min = int.MinValue;
int max = int.MaxValue;
Console.WriteLine($"The range of integers is {min} to {max}");
Console.WriteLine($"An example of overflow: {max + 1}, {max + 2}, {max + 3}");
// Long range
Console.WriteLine($"The range of longs is {long.MinValue} to {long.MaxValue}");
Console.WriteLine();
// Modulo operator
var number = 170;
var digit = (number/10) % 10;
Console.WriteLine($"The second digit of {number} is {digit}");
// Doubles
double first = 5;
double second = 4;
double third = 2;
double result = (first + second) / third;
Console.WriteLine(result);
Console.WriteLine($"The range of doubles is {double.MinValue} to {double.MaxValue}");
// Decimals
Console.WriteLine($"The range of decimals is {decimal.MinValue} to {decimal.MaxValue}");
// `M` is a suffix needed for decimals
decimal dividend = 1.0M;
decimal divider = 3.0M;
decimal decResult = dividend / divider;
Console.WriteLine(decResult);
Console.WriteLine();
// Math
double radius = 2.5;
double area = Math.PI * radius * radius;
Console.WriteLine(area);
}
}
// See https://aka.ms/new-console-template for more information
const int a = 5;
const int b = 6;
const bool result = (a + b > 10);
if (result)
Console.WriteLine("The answer is greater than 10.");
// while loop
var counter = 0;
while (counter < 10)
{
Console.WriteLine(counter);
counter++;
}
// do..while loop
do
{
Console.WriteLine("At least once");
} while(counter < 10);
// for loop
var sum = 0;
for (var i = 0; i < 10; i++)
{
if (i % 2 == 0)
{
sum += i;
Console.WriteLine($"{i} is an even number");
}
}
Console.WriteLine($"The sum of even numbers is {sum}");
// defining a list
var names = new List<string>{"<name>", "Anna", "Maksim", "Maria"};
// adding and removing items
names.Add("Daniel");
names.Remove("Maksim");
// iterating over the list
foreach (var name in names) {
Console.WriteLine($"Hello, {name.ToUpper()}");
}
// vs
for (var i = 0; i < names.Count; i++)
{
Console.WriteLine($"Hello, {names[i].ToUpper()}");
}
// working with indexes
Console.WriteLine(names[0]);
Console.WriteLine($"Found Maria at {names.IndexOf("Maria")}");
if (names.IndexOf("Bill") == -1)
Console.WriteLine($"Bill is not found");
// in-place sorting
names.Sort();
// Lists of other types ------------------------------------------------------------------
var fibonacciNumbers = new List<int>{1,1};
for (var i = 1; i < 19; i++)
{
var nextNumber = fibonacciNumbers[i - 1] + fibonacciNumbers[i];
fibonacciNumbers.Add(nextNumber);
}
var fibonacciSequence = string.Join(", ", fibonacciNumbers);
Console.WriteLine($"The first 20 numbers in the Fibonacci sequence: {fibonacciSequence}");
// BankAccount.cs
using System.Text;
namespace CSharp101;
public class BankAccount
{
public string Number { get; }
public string Owner { get; set; }
public decimal Balance
{
get
{
return _transactions.Sum(transaction => transaction.Amount);
}
}
private static int _accountNumberSeed = 1234567890;
private readonly List<Transaction> _transactions = new();
public BankAccount(string name)
{
this.Number = _accountNumberSeed.ToString();
_accountNumberSeed++;
this.Owner = name;
}
public void MakeDeposit(decimal amount, DateTime date, string note)
{
if (amount <= 0)
throw new ArgumentOutOfRangeException(nameof(amount), "Amount of deposit must be positive");
var deposit = new Transaction(amount, date, note);
_transactions.Add(deposit);
}
public void MakeWithdrawal(decimal amount, DateTime date, string note)
{
if (amount <= 0)
throw new ArgumentOutOfRangeException(nameof(amount), "Amount of deposit must be positive");
if (Balance - amount < 0)
throw new InvalidOperationException("Not sufficient funds for this withdrawal");
var withdrawal = new Transaction(-amount, date, note);
_transactions.Add(withdrawal);
}
public string GetAccountHistory()
{
var report = new StringBuilder();
report.AppendLine("Timestamp\t\t\tAmount\tNote");
foreach (var transaction in _transactions)
{
report.AppendLine($"{transaction.Date}\t{transaction.Amount}\t{transaction.Notes}");
}
return report.ToString();
}
}
// Transaction.cs
// namespace CSharp101;
public class Transaction
{
public decimal Amount { get; }
public DateTime Date { get; }
public string Notes { get; }
public Transaction(decimal amount, DateTime date, string notes)
{
this.Amount = amount;
this.Date = date;
this.Notes = notes;
}
}
// Program.cs
var account = new BankAccount("Andrey");
Console.WriteLine($"Account {account.Number} was created for {account.Owner} with ${account.Balance}");
account.MakeDeposit(1000, DateTime.Now, "Initial Balance");
account.MakeWithdrawal(10, DateTime.Now, "Buying a coffee");
try
{
account.MakeDeposit(-100, DateTime.Now, "XBOX");
}
catch (ArgumentOutOfRangeException e)
{
Console.WriteLine("Exception caught making a deposit with a negative amount");
Console.WriteLine(e);
}
Console.WriteLine(account.GetAccountHistory());
Console.WriteLine("------------------------------------------------");
Console.WriteLine($"Current balance: {account.Balance}");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment