dev/CSHARP/Lessons.md
deadzilla ace4d93bd8 3.6
2026-05-12 22:48:13 +05:00

199 lines
3.7 KiB
Markdown

###Классы и объекты
```
Person tom = new Person();
string personName = tom.name;
int personAge = tom.age;
Console.WriteLine($"name: {personName} age: {personAge}");
tom.name = "Tom";
tom.age = 37;
tom.Print();
class Person
{
public string name = "Undefined";
public int age;
public void Print()
{
Console.WriteLine($"Имя: {name} Возраст: {age}");
}
}
```
###Конструкторы, инициализаторы и деконструкторы
```
Person tom = new Person();
Person bob = new Person("Bob");
Person sam = new Person("Sam", 25);
tom.Print();
bob.Print();
sam.Print();
class Person
{
public string name;
public int age;
public Person()
{
name = "Unknown";
age = 18;
}
public Person(string n) { name = n; age = 18; }
public Person(string n, int a) { name = n; age = a; }
public void Print()
{
Console.WriteLine($"name: {name} age: {age}");
}
}
```
```
Person sam = new Person("Sam", 25);
sam.Print();
class Person
{
public string name;
public int age;
public Person()
{
name = "Unknown";
age = 18;
}
public Person(string name) { this.name = name; age = 18; }
public Person(string name, int age)
{
this.name = name;
this.age = age;
}
public void Print()
{
Console.WriteLine($"name: {name} age: {age}");
}
}
```
###Цепочка вызова конструкторов
```
Person sam = new Person("Sam", 25);
sam.Print();
class Person
{
public string name;
public int age;
public Person() : this("Unknown") { }
public Person(string name) : this(name, 18) { }
public Person(string name, int age)
{
this.name = name;
this.age = age;
}
public void Print() => Console.WriteLine($"Name: {name} Age: {age}");
}
```
###Первичные конструкторы
```
var tom = new Person("Tom", 38);
Console.WriteLine(tom);
public class Person(string name, int age)
{
public Person(string name) : this(name, 18) { }
public void Print() => Console.WriteLine($"name: {name}, age: {age}");
}
```
###Инициализаторы объектов
```
Person tom = new Person { name = "Tom", company = { title = "Microsoft" } };
tom.Print();
class Person
{
public string name;
public Company company;
public Person()
{
name = "Undefinde";
company = new Company();
}
public void Print() => Console.WriteLine($"name: {name} company: {company.title}");
}
class Company
{
public string title = "Unknown";
}
```
###Деконструкторы
```
Person person = new Person("Tom", 33);
(string name, int age) = person; //(_, int age) = person если нужен только возраст
Console.WriteLine(name);
class Person
{
string name;
int age;
public Person(string name, int age)
{
this.name;
this.age;
}
public void Deconstruct(out string personName, out int personAge)
{
personName = name;
personAge = age;
}
}
```
###Класс Program и метод Main. Программы верхнего уровня
```
string hello = "Hello";
Print(hello);
void Print(string message)
{
Console.WriteLine(message);
}
Person tom = new();
tom.SayHello();
class Person
{
public void SayHello() => Console.WriteLine("Hello");
}
```
###Структуры
```
struct Person
{
public string name;
public int age;
public void Print()
{
Console.WriteLine($"Name: {name} Age: {age}");
}
}
```
###Типы значений и ссылочные типы
###Область видимости переменных (контекст)