This repository has been archived on 2024-08-23. You can view files and clone it, but cannot push or open issues or pull requests.
lessons/C#/C#.md
2023-09-15 09:40:57 +00:00

1.7 KiB

C#

Project file

Project file - файл для конфигурации проекта в формате xml

Поля:

  • OutputType (Exe | Dll) - во что компилировать
  • TargetFramework (net7.0) - версия .NET

Переменные

string name = "Tom";
string name2;
name2 = "Bob";

Константы

const string NAME = "Tom";

Типы данных

  • bool
  • sbyte (signed byte)
  • byte
  • short - 16 байт
  • ushort (unsigned)
  • int - 32 байта
  • uint
  • long - 64 байта
  • ulong
  • char - 16 байт. Знак unicode
  • float 32 байт
  • double - 64 байта
  • decimal - 128 бит
  • string
  • object - аля Python Object

Числа в формате 3.14 по умолчанию double. Если нужен float или decimal, то используются суффиксы

float a = 3.14f;
decimal b = 3.14m;

Для целочисленных по умолчанию int

int a = 5;
uint b = 5u;
long c = 5l;
ulong d = 5ul;

Неявная типизация

var hello = "Hello world";

Для var объявление и инициализация должны идти вместе

// Так низя: ошибка
var c;
c = 5;

Вывод в терминал

Console.WriteLine("Привет");
Console.Write("Привет ");
Console.Write("Мир");

Выведет:

Привет
Привет мир

Форматированный вывод

string name = "John";
string age = 34;
Console.WriteLine($"{name} is {age} years old")

Ввод данных

string? name = Console.ReadLine();
int foo = Convert.ToInt32(Console.ReadLine());

Простые операторы

  • +
  • -
  • *
  • /
  • %