This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var name = Guid.NewGuid() | |
.ToString() | |
.Aggregate("", (acc, x) => | |
{ | |
return acc.Length switch | |
{ | |
< 2 when char.IsLetter(x) => $"{acc}{x.ToString().ToUpper()}", | |
>= 2 and < 5 when char.IsDigit(x) => $"{acc}{x.ToString()}", | |
_ => acc | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var builder = WebApplication.CreateBuilder(args); | |
builder.Services.AddControllers(); | |
var app = builder.Build(); | |
app | |
.UseRouting() | |
.UseEndpoints(x => x.MapControllers()); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var retryPolicy = Policy | |
.Handle<ApiException>() | |
.WaitAndRetryAsync(3, x => TimeSpan.FromSeconds(Math.Pow(2, x))) | |
.AsAsyncPolicy<HttpResponseMessage>(); | |
services | |
.AddRefitClient<IHttpClient>() | |
.ConfigureHttpClient(x => x.BaseAddress = new Uri("https://localhost:8080")) | |
.AddPolicyHandler(retryPolicy); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// before C#10 and .NET6 | |
// | |
public void GetSomething(string id) | |
{ | |
if (id is null) | |
{ | |
throw new ArgumentNullException(nameof(id)); | |
} | |
// ... and here your code to get some data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web; | |
public static bool IsValidUri(string input) => Uri.IsWellFormedUriString(input, UriKind.Absolute); | |
// Input: https://www.youtube.com/watch?v=dfzBMxXQUOc | |
// Output: dfzBMxXQUOc | |
static string? GetYouTubeVideoId(string input) | |
=> HttpUtility.ParseQueryString(new Uri(input).Query).Get("v"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Net; | |
using System.Linq; | |
public static bool IsValidIpAddress(string input) | |
=> IPAddress.TryParse(input, out var ipAddress) && ipAddress.ToString() == input; | |
public static bool IsValidIpAddress(string input) | |
=> input.Split(".") switch | |
{ | |
{Length: 4} parts => parts.All(x => int.TryParse(x, out var y) && y is >= 0 and <= 255), |