interface ISpeaking {
public void Speak();
}
public class Dog: ISpeaking {
public void Speak() => Console.WriteLine("Гав!");
}
public class Cat: ISpeaking {
public void Speak() => Console.WriteLine("Мяу!");
}
public static void Main() {
ISpeaking[] speakers = new ISpeaking[] {new Cat(), new Dog()};
foreach(ISpeaking speaker in speakers)
speaker.Speak();
}
static void PrintAll(string[] array) {
foreach(var e in array)
Console.WriteLine(e);
}
static void PrintAll(List<string> list) {
foreach(var e in list) { // Дублирование!
Console.WriteLine(e);
}
// Можно использовать и для string[], и для List<string>, и даже для того, о чём мы ещё не знаем!
static void PrintAll(IEnumerable<string> collection) {
foreach(var e in collection) {
Console.WriteLine(e);
}
}
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/net5.0/Demo.dll",
"args": [],
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"stopAtEntry": false
}
]
}
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "shell",
"args": [
"build",
// Ask dotnet build to generate full paths for file names.
"/property:GenerateFullPaths=true",
// Do not generate summary otherwise it leads to duplicate errors in Problems panel
"/consoleloggerparameters:NoSummary"
],
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
var json = "{\"id\":1,\"number\":42,\"bagGUID\":null}";
var data = JsonSerializer.Deserialize<Barcode>(json);
Console.WriteLine(data!.ToString()); // выведет "Barcode { Id = 1, Number = 42, BagGuid = }"
record Barcode
{
[JsonPropertyName("id")]
public int Id { get; init; }
[JsonPropertyName("number")]
public int Number { get; init; }
[JsonPropertyName("bagGUID")]
public Guid? BagGuid { get; init; }
}
value=_FirstName
HashSet<string>
public class AccountType {
[JsonPropertyName("id")]
public string Id { get; set; }
[JsonPropertyName("title")]
public string Title { get; set; }
}
public class Balance {
[JsonPropertyName("amount")]
public decimal Amount { get; set; }
[JsonPropertyName("currency")]
public int Currency { get; set; }
}
public class Account {
[JsonPropertyName("alias")]
public string Alias { get; set; }
[JsonPropertyName("fsAlias")]
public string FsAlias { get; set; }
[JsonPropertyName("bankAlias")]
public string BankAlias { get; set; }
[JsonPropertyName("title")]
public string Title { get; set; }
[JsonPropertyName("type")]
public AccountType Type { get; set; }
[JsonPropertyName("hasBalance")]
public bool HasBalance { get; set; }
[JsonPropertyName("balance")]
public Balance Balance { get; set; }
[JsonPropertyName("currency")]
public int Currency { get; set; }
}
public class AccountsInfo {
[JsonPropertyName("accounts")]
public Account[] Accounts { get; set; }
}
var data = JsonSerializer.Deserialize<AccountsInfo>(json);
data.Accounts[0].Balance.Amount; //баланс первого счёта