class Program
{
static void Main(string[] args)
{
var results = BenchmarkRunner.Run<IsEvenBenchmark>();
Console.ReadKey();
}
}
[MemoryDiagnoser]
[SimpleJob(RuntimeMoniker.Net60)]
[SimpleJob(RuntimeMoniker.Net472)]
public class IsEvenBenchmark
{
[Benchmark]
[Arguments(42)]
public bool IsEven(int i)
{
return i % 2 == 0;
}
}
void Example1(string id)
{
if (id is null)
{
throw new ArgumentNullException(nameof(id));
}
}
void Example2(string id)
{
ArgumentNullException.ThrowIfNull(id);
}
Process process = Process.GetCurrentProcess();
string path = process.MainModule.FileName;
int id = process.Id;
Console.WriteLine(path);
Console.WriteLine(id);
string path2 = Environment.ProcessPath;
int id2 = Environment.ProcessId;
using System.Security.Cryptography;
//for
//- key generation
//- nonces
//- salts in certain signature schemes
var random = RandomNumberGenerator.
GetInt32(1, 1000000);
var bytes = RandomNumberGenerator.
GetBytes(255);
using PeriodicTimer timer =
new(TimeSpan.FromSeconds(2));
while (await timer.WaitForNextTickAsync())
{
Console.WriteLine(DateTime.UtcNow.Second);
}
unsafe
{
byte* buffer = (byte*)NativeMemory.Alloc(255);
NativeMemory.Free(buffer);
}
PriorityQueue queue = new PriorityQueue();
queue.Enqueue("A", 0);
queue.Enqueue("B", 3);
queue.Enqueue("C", 2);
queue.Enqueue("D", 1);
while (queue.TryDequeue(out string item, out int priority))
{
Console.WriteLine($"Popped Item : {item}. " +
$"Priority Was : {priority}");
}
PriorityQueue<string, int> queue = new PriorityQueue<string, int>();
queue.Enqueue("A", 0);
queue.Enqueue("B", 3);
queue.Enqueue("C", 2);
queue.Enqueue("D", 1);
queue.Enqueue("E", 0);
queue.Enqueue("F", 1);
while (queue.TryDequeue(out string item, out int priority))
{
Console.WriteLine($"Popped Item : {item}. " +
$"Priority Was : {priority}");
}
var dateOnly = new DateOnly
(2021, 8, 20);
var timeOnly = new TimeOnly
(08, 43, 57);
DateTime dateTime = dateOnly.
ToDateTime(timeOnly);
uint number = 235;
if (!BitOperations.IsPow2(235))
{
number =
BitOperations.RoundUpToPowerOf2(number);
}
Console.WriteLine(number);
var numbers = new List<int> { 1, 2 };
Console.WriteLine(numbers.Capacity < 100);
numbers.EnsureCapacity(100);
Console.WriteLine(numbers.Count == 2);
Console.WriteLine(numbers.Capacity == 100);
numbers.EnsureCapacity(50);
Console.WriteLine(numbers.Capacity == 100);
for (int i = numbers.Count; i < 100; i++)
{
numbers.Add(i);
}
Console.WriteLine(numbers.Count == 100);
Console.WriteLine(numbers.Capacity == 100);
var list = Enumerable.Range(1, 100);
foreach (var chunk in list.Chunk(10))
{
Console.WriteLine("This next chunk");
foreach (var item in chunk)
{
Console.WriteLine(item);
}
}
var products1 = new[] {
(Name: "Q", Price: 17),
(Name: "W", Price: 100),
(Name: "E", Price: 70),
(Name: "Y", Price: 17),
};
var products2 = new[] {
(Name: "R", Price: 17),
(Name: "T", Price: 12), };
var b1 = products1.MaxBy(p => p.Price)
.Name == "W";
var b2 = products1.MinBy(p => p.Price)
.Name == "Q";
IEnumerable<int> seq1 = new[] { 0, 1, 2, 3, 4 };
seq1.TryGetNonEnumeratedCount(out int count1);
Console.WriteLine(count1 == 5);
IEnumerable<int> seq2 = new
BetterCollectionBeacuseItsMine
<int>();
var result =
seq2.TryGetNonEnumeratedCount(out int count2);
Console.WriteLine(result);
var arr2 = new[] { 0, 2, 4, 6, 8, 10 };
var c1 = arr2.FirstOrDefault
(x => x > 11) == 0;
var c2 = arr2.FirstOrDefault
(x => x > 11, -1) == -1;
var arr = new[] { 0, 1, 2, 3, 4, 5 };
var a1 = arr.ElementAt(^2) == 4;
var a2 = arr.ElementAtOrDefault
(^10) == default;
var a3 = arr.Take(^2..).
SequenceEqual(new[] { 4, 5 });
await ExampleAsync();
Console.Read();
static async Task<int> ExampleAsync()
{
var operation = SomeLongRunningOperationAsync();
return await operation.WaitAsync
(TimeSpan.FromSeconds(3));
}
static async Task<int> SomeLongRunningOperationAsync()
{
await Task.Delay(5000);
return 1;
}
static async Task Example2Async()
{
var urlsToDownload = new[]
{
"https://cezarywalenciuk.pl/",
"https://twitter.com/WalenciukC",
};
var client = new HttpClient();
await Parallel.ForEachAsync(urlsToDownload,
async (url, token) =>
{
var response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
Console.WriteLine
(await response.Content.ReadAsStringAsync());
}
});
}
public class Game
{
[JsonPropertyOrder(1)]
public int Year { get; set; }
public string Name { get; set; } // 0
//serialize after Year
[JsonPropertyOrder(2)]
public string Company { get; set; }
// serialize before other properties
[JsonPropertyOrder(-1)]
public int Id { get; set; }
}
Game game = new()
{
Company = "Konami",
Name = "Silent Hill",
Id = 564,
Year = 1999
};
JsonSerializerOptions options =
new () { WriteIndented = true};
string json = JsonSerializer
.Serialize(game,options);
Console.WriteLine(json);
JsonNode jNode = JsonNode.Parse
("{\"array\":[1,6,7],\"name\":\"Cezary\"}");
string name = (string)jNode["name"];
Console.WriteLine(name);
string name1 = jNode["name"]
.GetValue<string>();
int number = jNode["array"][2]
.GetValue<int>();
var jObject = new JsonObject()
{
["name"] = "Konrad",
["array"] = new JsonArray(2, 3, 4, 5)
};
string json = jObject.ToJsonString();
Console.WriteLine(json);
public class TShirt : IJsonOnDeserialized,
IJsonOnDeserializing,
IJsonOnSerializing,
IJsonOnSerialized
{
public void OnDeserialized()
{ }
public void OnDeserializing()
{ Validate(); }
public void OnSerialized()
{ }
public void OnSerializing()
{ Validate(); }
public string Color { get; set; }
private void Validate()
{
if (Color is null)
throw new InvalidOperationException
($"{nameof(Color)} Can not be null");
}
}
public static async IAsyncEnumerable<int>
GetNumbersAsync()
{
for (int i = 0; i < 5; i++)
{
await Task.Delay(2000);
yield return i;
}
}
JsonSerializerOptions options = new()
{ WriteIndented = true };
using Stream stream = Console.OpenStandardOutput();
var data = new { Number = GetNumbersAsync() };
await JsonSerializer.SerializeAsync
(stream, data, options);
public class ToRemember
{
public string Conference { get; set; }
}
JsonSerializerOptions options = new()
{ WriteIndented = true };
using Stream outputStream = Console.OpenStandardOutput();
ToRemember to = new() { Conference = "5Developers" };
JsonSerializer.Serialize<ToRemember>(outputStream
,to,options);
string json = "{\"Conference\":\"4Developers\"}";
byte[] bytes = Encoding.UTF8.GetBytes(json);
using MemoryStream ms = new MemoryStream();
ToRemember toRemember =
JsonSerializer.Deserialize<ToRemember>(ms);
Console.WriteLine(toRemember.Conference);
namespace ViewerApp.Models
{
public class Viewer
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? StreetAddress { get; set; }
}
}
namespace ViewerApp.Models;
public class Viewer2
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? StreetAddress { get; set; }
}
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
Console.WriteLine("Daj pozytwna ocene");
await Task.Delay(2000);
List<string> list = new List<string>();
list.Add("Tej prelekcj");
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
//using System;
//using System.Collections.Generic;
//using System.Threading.Tasks;
Console.WriteLine("Daj pozytwna ocene");
await Task.Delay(2000);
List<string> list = new List<string>();
list.Add("Tej prelekcj");
// <auto-generated/>
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Threading;
global using global::System.Threading.Tasks;
// <autogenerated />
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Threading;
global using global::System.Threading.Tasks;
global using global::System.Net.Http.Json;
global using global::Microsoft.AspNetCore.Builder;
global using global::Microsoft.AspNetCore.Hosting;
global using global::Microsoft.AspNetCore.Http;
global using global::Microsoft.AspNetCore.Routing;
global using global::Microsoft.Extensions.Configuration;
global using global::Microsoft.Extensions.DependencyInjection;
global using global::Microsoft.Extensions.Hosting;
global using global::Microsoft.Extensions.Logging;
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Using Include="System.Text.Json" />
<Using Include="System.Text.Json.Serialization" />
<Using Remove="System.IO" />
<Using Include="System.Console" Static="True" />
<Using Include="System.DateTime" Alias="DT" />
</ItemGroup>
</Project>
global using ViewerApp.Models;
global using static System.Console;
global using DT = System.DateTime;
using System;
namespace ConsoleApplication87
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.MapGet("/", () => "Hello World!");
app.Run();
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<HelloService>
(new HelloService());
var app = builder.Build();
app.MapGet("/", () => "Daj dobrą ocenę");
app.MapGet("/hello", (HttpContext context, HelloService service)
=> service.SayHello(context.Request.Query["name"]
.ToString()));
await app.RunAsync();
public class HelloService
{
public string SayHello(string name)
{
return $"Hello {name}";
}
}
public static class Routes
{
public static IEndpointRouteBuilder UseHelloEndpoint
(this IEndpointRouteBuilder endpoints)
{
endpoints.MapPost("api/hello", async context =>
{
});
return endpoints;
}
}
public class EndpointDefinition : IEndpointDefinition
{
public void DefineEndpoints(WebApplication app)
{
app.MapPost("api/hello", async context =>
{
});
}
public void DefineServices(IServiceCollection services)
{
}
}
public interface IEndpointDefinition
{
void DefineServices(IServiceCollection services);
void DefineEndpoints(WebApplication app);
}
public static class EndPointDefinitionExtension
{
public static void AddEndpointDefinitions(
this IServiceCollection services,params Type[] scanMarkers)
{
var endpoints = new List<IEndpointDefinition>();
foreach (var scanMarker in scanMarkers)
{
endpoints.AddRange(
scanMarker.Assembly.ExportedTypes
.Where(x => typeof(IEndpointDefinition)
.IsAssignableFrom(x) && (!x.IsInterface && !x.IsAbstract))
.Select(Activator.CreateInstance)
.Cast<IEndpointDefinition>()
);
}
foreach (var endpoint in endpoints)
{
endpoint.DefineServices(services);
}
services.AddSingleton
(endpoints as IReadOnlyCollection<IEndpointDefinition>);
}
public static void UseEndpointDefinitions(this WebApplication app)
{
var defs = app.Services.
GetRequiredService<IReadOnlyCollection<IEndpointDefinition>>();
foreach (var def in defs)
{
def.DefineEndpoints(app);
}
}
}
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointDefinitions
(typeof(EndpointDefinition));
var app = builder.Build();
app.UseEndpointDefinitions();
app.Run();
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSignalR();
builder.Services.AddCors(options =>
{
options.AddPolicy("CorsPolicy", builder => builder
.WithOrigins("http://localhost:56675")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
var app = builder.Build();
app.UseCors("CorsPolicy");
app.MapHub<MyHub>("/chat");
app.MapHub<ChartHub>("/chart");
app.Run();
public class MyHub : Hub
{
public async IAsyncEnumerable<string> Streaming
(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
if (_random.Next(0, 10) < 5)
yield return DateTime.UtcNow.ToString("hh:mm:ss");
try
{
await Task.Delay(1000,stoppingToken);
}
catch (OperationCanceledException)
{
yield break;
}
}
}
private Random _random = new Random();
}
using Microsoft.AspNetCore.SignalR.Client;
var uri = "http://localhost:5000/chat";
await using var connection = new HubConnectionBuilder()
.WithUrl(uri)
.Build();
await connection.StartAsync();
await foreach (var item in
connection.StreamAsync<string>("Streaming"))
{
Console.WriteLine(item);
}
Drink drink = new () { Fat = 20, TotalVolume= 200};
Drink green = drink with { Fat = 40};
public struct Drink
{
public int Fat { get; set; }
public int TotalVolume { get; set; }
}
public struct Drink
{
public int Fat { get; set; } = 1;
public int TotalVolume { get; set; } = 2;
}
record struct DrinkRecord(int fat, int totalVolume);
var a1 = string () => string.Empty;
var a2 = int () => int.MaxValue;
var a3 = static void () => { };
var a4 = string? () => null;
public static void Example<T>()
{
var a5 = T () => default;
}
Func<int> f1 = [MyAttribute] () => { return 0; };
Func<int> f2 = [return: MyAttribute] () => { return 0; };
Func<int,int> f3 = ([MyAttribute] x)=> { return x; };
class MyAttribute : Attribute
{
}
int a;int b;
(a, int r, int g,b) = (255, 100, 0, 150);
public record Game
{
public sealed override string ToString()
{
return base.ToString();
}
}
var speech = new { Speaker = new { Name = "C" } };
//C# 9
if (speech is { Speaker: { Name :"C"} }) { }
//C# 10
if (speech is { Speaker.Name: "C"}) { }
const string NAME = "MyApplication";
const string FULLNAME = $"{NAME} 1.20";
const double VERSIONP = 1.20;
//const string FULLNAME2 = $"{NAME} {VERSIONP}";
public interface ICreatable<TSelf, TArg1, TArg2, TArg3>
where TSelf : ICreatable<TSelf, TArg1, TArg2, TArg3>
{
static abstract TSelf Create(TArg1 arg1,
TArg2 arg2, TArg3 arg3);
}
public class Game : ICreatable<Game, string, string, int>
{
public Game(string name, string desc, int year)
{
Name = name; Description = desc; Year = year;
}
public string Name { get; set; }
public string Description { get; set; }
public int Year { get; set; }
public static Game Create
(string name, string desc, int year)
{
return new Game(name, desc, year);
}
}
Game.Create("a", "a", 1);
static T Add<T>(T left, T right) where T : INumber<T>
{
return left + right;
}
static T ParseInvariant<T>(string s) where T : IParseable<T>
{
return T.Parse(s,
CultureInfo.InvariantCulture);
}
Console.Write("First number: ");
var left = ParseInvariant<float>(Console.ReadLine());
Console.Write("Second number: ");
var right = ParseInvariant<float>(Console.ReadLine());
Console.WriteLine($"Result: {Add(left, right)}");