<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>
using System;
namespace ConsoleTopLevelCallsNot
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.WriteLine(Substract(33, 11));
}
private static double Substract(double x, double y)
{
return x - y;
}
}
}
using System;
Console.WriteLine("Hello World!");
Console.WriteLine(Substract2(33, 11));
static double Substract2(double x, double y)
{
return x - y;
}
using System;
namespace ConsoleTopLevelCallsNot
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{
System.Console.WriteLine("Please enter a numeric argument.");
}
}
}
}
using System;
if (args.Length == 0)
{
System.Console.WriteLine("Please enter a numeric argument.");
}
public class PirateModel
{
public int Id { get; set; }
public string CrewName { get; set; }
public string Name { get; set; }
}
public static class InitialSetterExample
{
public static void Run()
{
PirateModel m = new PirateModel
{ Name = "Luffy", CrewName = "StrawHats", Id = 1 };
m.Id = 2;
m.Name = "Zorro";
Console.WriteLine($"Hello {m.Name}, Id : {m.Id}");
}
}
public class PirateModel
{
public int Id { get; private set; }
public string CrewName { get; set; }
public string Name { get; set; }
}
public class PirateModel
{
public int Id { get; private set; }
public string CrewName { get; set; }
public string Name { get; set; }
public void UpdateId(int newId)
{
Id = newId;
}
}
public class PirateModel
{
public int Id { get; init; }
public string CrewName { get; set; }
public string Name { get; set; }
}
PirateModel m3 = new();
PirateModel m4 = new()
{ Name = "Zorro", CrewName = "StrawHats", Id = 2 };
//var m5 = new();
PirateModel m6;
//1000 linjek kodu później
m6 = new();
public class Weather
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public string Desc { get; set; }
}
public static List<Weather> CreateExample()
{
DateTime now = DateTime.Now;
var rng = new Random();
var exampleCollection = Enumerable.Range(1, 14).Select
(index => new Weather
{
Date = now.AddDays(index),
TemperatureC = rng.Next(-10, 40)
}
).ToList();
return exampleCollection;
}
foreach (var weather in exampleList)
{
weather.Desc = weather.TemperatureC switch
{
<= 6 and > 1 => "Nie ciekawie. Ubrać sie trzeba",
10 or 9 or 8 or 7 => "Zimno",
11 => "Uwaga Łatwo zachorować",
12 => "Uwaga Kurtka",
13 => "Uwaga",
<= 15 => "Super",
<= 20 => "Ok",
<= 28 => "Gorąco",
>= 30 => "Upał",
_ => "Nie wiem"
};
}
public class Pizza
{
public string Name { get; set; }
public bool HasMeat { get; set; }
public bool HasAnanas { get; set; }
public void Deconstruct(out string name, out bool hasMeat, out bool hasAnanas)
{
name = Name;
hasMeat = HasMeat;
hasAnanas = HasAnanas;
}
}
public class Pizza
{
public string Name { get; set; }
public bool HasMeat { get; set; }
public bool HasAnanas { get; set; }
public void Deconstruct(out string name, out bool hasMeat, out bool hasAnanas)
{
name = Name;
hasMeat = HasMeat;
hasAnanas = HasAnanas;
}
public string Desc { get; set; }
}
public static List<Pizza> CreateExample()
{
var rng = new Random();
var exampleCollection = Enumerable.Range(1, 14).Select
(index => new Pizza
{
Name = "P" + index.ToString() + rng.Next(1, 10).ToString(),
HasMeat = rng.NextDouble() >= 0.5,
HasAnanas = rng.NextDouble() >= 0.5
}
).ToList();
return exampleCollection;
}
Pizza a = new Pizza() { HasAnanas = false, HasMeat = true, Name = "Marinara" };
Pizza b = new Pizza() { HasAnanas = false, HasMeat = true, Name = "Peperoni" };
var (name, hasananas, hasmeat) = a;
var (name2, hasananas2, hasmeat2) = b;
var exampleList = CreateExample();
Pizza a = new Pizza() { HasAnanas = false, HasMeat = true, Name = "Marinara" };
Pizza b = new Pizza() { HasAnanas = false, HasMeat = true, Name = "Peperoni" };
exampleList.Add(b); exampleList.Add(a);
foreach (var pizza in exampleList)
{
pizza.Desc = pizza switch
{
("1", true, false) => "Taką zjem",
("2", true, true) => "Ujdzie",
("Peperoni", _, _) => "Moja ulubiona",
("Marinara", _, _) => "Często jadam",
(_, _, true) => "Tej nie zjem",
_ => "Nie wiem"
};
}
var exampleList = CreateExample();
Pizza a = new Pizza() { HasAnanas = false, HasMeat = true, Name = "Marinara" };
Pizza b = new Pizza() { HasAnanas = false, HasMeat = true, Name = "Peperoni" };
exampleList.Add(b); exampleList.Add(a);
foreach (var pizza in exampleList)
{
pizza.Desc = pizza switch
{
("2", _, var czymaAnans) => $"Pizza z Ananasem : {czymaAnans}",
{ HasMeat : true} => "Ma mieso"
};
}
var exampleList = CreateExample();
Pizza a = new Pizza() { HasAnanas = false, HasMeat = true, Name = "Marinara" };
Pizza b = new Pizza() { HasAnanas = false, HasMeat = true, Name = "Peperoni" };
exampleList.Add(b); exampleList.Add(a);
foreach (var pizza in exampleList)
{
pizza.Desc = pizza switch
{
("2", _, var czymaAnans) => $"Pizza z Ananasem : {czymaAnans}",
};
}
var exampleList = CreateExample();
Pizza a = new Pizza() { HasAnanas = false, HasMeat = true, Name = "Marinara" };
Pizza b = new Pizza() { HasAnanas = false, HasMeat = true, Name = "Peperoni" };
exampleList.Add(b); exampleList.Add(a);
foreach (var pizza in exampleList)
{
pizza.Desc = pizza switch
{
("2", _, var czymaAnans) => $"Pizza z Ananasem : {czymaAnans}",
(HasMeat: true) => $"Ma mieso",
_ => throw new NotImlementedException()
};
}
var exampleList = CreateExample();
Pizza a = new Pizza() { HasAnanas = false, HasMeat = true, Name = "Marinara" };
Pizza b = new Pizza() { HasAnanas = false, HasMeat = true, Name = "Peperoni" };
exampleList.Add(b); exampleList.Add(a);
foreach (var pizza in exampleList)
{
pizza.Desc = (pizza.HasMeat, pizza.HasAnanas) switch
{
(true, false) => "Super",
(false, true) => "Poprostu nie",
(false, false) => "Bez mięsa ?"
};
}
public class GreenTest : Test
{
}
public class RedTest : Test
{
}
public class Test
{
}
public static List<Test> CreateExample()
{
var rng = new Random();
List<Test> tests = new List<Test>();
for (int i = 0; i < 15 ; i++)
{
if (rng.Next(1,100) % 2 == 0)
tests.Add(new GreenTest());
else
tests.Add(new RedTest());
}
return tests;
}
var exampleList = CreateExample();
foreach (var test in exampleList)
{
string outcome = test switch
{
GreenTest => "Postive",
RedTest => "Negative",
_ => "unknow"
};
Console.WriteLine(outcome);
}
public class GreenTest : Test
{
}
public class RedTest : Test
{
}
public class Test
{
public bool IsValid { get; set; }
public DateTimeOffset TestedOn { get; set; }
public void Deconstruct(out int minutesSinceTest, out bool isValid)
{
minutesSinceTest = (int)(DateTimeOffset.UtcNow - TestedOn).TotalMinutes;
isValid = IsValid;
}
}
var exampleList = CreateExample();
foreach (var test in exampleList)
{
string outcome = test switch
{
( < 60 * 24 * 2, false) => "Nie poprawny test których jest młodszy niż 2 dni",
GreenTest(> 60 * 24, true) => "Postive, ale warto go po 24 godzinach zrobić ponownie",
_ => "unknow"
};
Console.WriteLine(outcome);
}
PirateModel m1 = new PirateModel
{ Name = "Luffy", CrewName = "StrawHats", Id = 1 };
PirateModel m2 = new()
{ Name = "Zorro", CrewName = "StrawHats", Id = 2 };
PirateModel m3 = null;
if (m3 is null)
{
}
if (m2 is not null)
{
}
//if (m1 is not m2)
//{
//}
public record Record1(int FirstNumber, int SecondNumber);
//Czy Rekordy są typem wartościowym,
//Czy typem referencyjnym?
Record1 rA = new(1, 2);
Record1 rAtest = rA;
Console.WriteLine(ReferenceEquals(rA, rAtest));
//true
//Czy rekord jako klasa wygląda tak
public class Record2
{
public string FirstNumber { get; set; }
public string SecondNumber { get; set; }
}
//Podobne do tej klasy, ale to jednak nie to samo
//o tym za chwilę
public class Record3
{
public string FirstNumber { get; init; }
public string SecondNumber { get; init; }
public Record3(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
}
//To jest poprawny rekord
public record Record4
{
public int Number1 { get; set; }
public int Number2 { get; set; }
}
public record Record1(string FirstName, string LastName);
//Jest immutable czyli wartości w nim nie mogą być zmienione
//Tutaj jest problem z nie jasnym działem rekordu
public record Record4
{
public int Number1 { get; set; }
public int Number2 { get; set; }
}
//rekord to klasa tylko innaczej napisana
public record Record1(int FirstNumber, int SecondNumber);
public class Class3
{
public int FirstNumber { get; init; }
public int SecondNumber { get; init; }
public Class3(int firstNumber, int secondNumber)
{
FirstNumber = firstNumber;
SecondNumber = secondNumber;
}
}
Record1 rA = new(1, 2);
Record1 rB = new(1, 2);
Record1 rC = new(100, 200);
Class3 classA = new(1, 2);
Class3 classB = new(1, 2);
Class3 classC = new(100, 200);
Console.WriteLine($"Record1 rA = new(1, 2);");
Console.WriteLine($"Record1 rB = new(1, 2);");
Console.WriteLine($"Check if they are Equal : {Equals(rA, rB)}");
Console.WriteLine("***");
Console.WriteLine($"Class3 classA = new(1, 2);");
Console.WriteLine($"Class3 classB = new(1, 2);");
Console.WriteLine($"Check if they are Equal : {Equals(classA, classB)}");
Console.WriteLine($"Record1 rA = new(1, 2);");
Console.WriteLine($"Record1 rB = new(1, 2);");
Console.WriteLine($"Check if they are Equal : {Equals(rA, rB)}");
Console.WriteLine($"Check if they are ReferenceEquals :" +
$"{ ReferenceEquals(rA, rB)}");
Console.WriteLine($"Record1 rA = new(1, 2);");
Console.WriteLine($"Record1 rB = new(1, 2);");
Console.WriteLine($"Check if they are Equal : {Equals(rA, rB)}");
Console.WriteLine($"Check if they are ReferenceEquals
: {ReferenceEquals(rA, rB)}");
Console.WriteLine($"Check if they are == : {rA == rB}");
Console.WriteLine($"Record1 rA = new(1, 2);");
Console.WriteLine($"Record1 rB = new(1, 2);");
Console.WriteLine($"Check if they are Equal : {Equals(rA, rB)}");
Console.WriteLine($"Check if they are ReferenceEquals :
{ReferenceEquals(rA, rB)}");
Console.WriteLine($"Check if they are == : {rA == rB}");
Console.WriteLine($"Hash Code rA: {rA.GetHashCode()}");
Console.WriteLine($"Hash Code rB: {rB.GetHashCode()}");
Console.WriteLine($"Record1 rC = new(100, 200);");
Console.WriteLine($"Hash Code rC: {rC.GetHashCode()}");
Console.WriteLine($"Check if they are != : {rA != rC}");
Console.WriteLine($"Record3 classA = new(1, 2);");
Console.WriteLine($"Record3 classB = new(1, 2);");
Console.WriteLine($"Check if they are Equal : {Equals(classA, classB)}");
Console.WriteLine($"Check if they are ReferenceEquals :
{ReferenceEquals(classA, classB)}");
Console.WriteLine($"Check if they are == : {classA == classB}");
Console.WriteLine($"Hash Code rA: {classA.GetHashCode()}");
Console.WriteLine($"Hash Code rB: {classB.GetHashCode()}");
Console.WriteLine($"Record3 classC = new(100, 200);");
Console.WriteLine($"Hash Code rC: {classC.GetHashCode()}");
Console.WriteLine($"Check if they are != : {classA != classC}");
Record1 rA = new(1, 2);
var (a, b) = rA;
public class Record3
{
public int FirstNumber { get; init; }
public int SecondNumber { get; init; }
public Record3(int firstNumber, int secondNumber)
{
FirstNumber = firstNumber;
SecondNumber = secondNumber;
}
public void Deconstruct(out int FirstNumber, out int SecondNumber)
{
FirstNumber = this.FirstNumber;
SecondNumber = this.SecondNumber;
}
}
Record1 rA = new(1, 2);
Record1 rAtest = rA;
Console.WriteLine(ReferenceEquals(rA, rAtest));
Record1 newRa = rA with
{
SecondNumber = 200
};
public record Data<T>(int id, T Data);
public interface IProduct<T>
{
T Data { get; }
}
public record Game<T> : Audit ,IProduct<T>
{
public T Data { get; init; }
}
public partial record Data(string Name);
public record Data([property:JsonPropertyName("dataName")] string Name,
[property:JsonPropertyName("dataCity")]string City);
public record Data([field:Foo("field")][property:Foo("prop")] string Name);
[AttributeUsage(AttributeTargets.All)]
class MyAttribute : Attribute
{
public MyAttribute(string _) { }
}
public record Data([My("param")][field:My("field")][property:My("prop")] string Name);
public record Numbers
{
[JsonPropertyName("One")]
public string First { get; init; }
[JsonPropertyName("Two")]
public string Second { get; init; }
}
//To jest tutaj problem z nie jasnym działem rekordu
public record Record4
{
public int Number1 { get; set; }
public int Number2 { get; set; }
}
public record Record5(int FirstNumber, int SecondNumber);
public record Record6(int FirstNumber, int SecondNumber)
{
internal int SecondNumber { get; init; } = SecondNumber;
}
public record Record7(int FirstNumber, int SecondNumber)
{
internal int SecondNumber { get; init; } = SecondNumber;
public string JavaScriptLikeSumOfNumbers { get => $"{FirstNumber}{SecondNumber}"}
}
public record Record8(int FirstNumber, int SecondNumber)
{
public (int,int) ReturnHasCodes()
{
return (FirstNumber.GetHashCode(), SecondNumber.GetHashCode());
}
}
nint nativeInt = 55;
Console.WriteLine(nint.MaxValue);
Compiled in x86 Configuration
Output: 2147483647
Compiled in x64 Configuration
Output: 9223372036854775807
Func<int,int,int> zero = (_,_) => 0;
partial class Doing
{
internal partial bool DoSomething(string s, out int i);
}
partial class Doing
{
internal partial bool DoSomething(string s, out int i)
{
i = 0;
return true;
}
}
public virtual Cat GetCat()
{
// This is the parent (or base) class.
return new Cat();
}
public override Cat GetCat
{
//You can return the child class, but still return a Cat
return new Tigger();
}
public virtual Cat GetCat()
{
// This is the parent (or base) class.
return new Cat();
}
public override Tiger GetCat()
{
return new Tigger();
}
public static class Extensions
{
public static IEnumerator<T> GetEnumerator<T> (this IEnumerator<T> enumerator) =>
}