Структуры C#

May 2, 2021 00:00 · 146 words · 1 minute read

Описание структуры, кортежи, перечисления

Перечисления enum 🔗

enum Time : byte
{
    Morning,
    Afternoon,
    Evening,
    Night
}

Кортежи 🔗

(int,int) t1 = (5, 10);
(int x, int y) t2 = (x:1, y:2);
var t3 = (x:3, y:4);
private static (int, int) GetValues()
{
    var result = (1, 3);
    return result;
}

Структуры 🔗

struct User
{
	public string name;
	public int age;
	public void DisplayInfo()
	{
	Console.WriteLine(
	$"Name: {name}  Age: {age}");
	}
}
User tom;
tom.name = "Tom";
tom.age = 34;
tom.DisplayInfo();

Классы 🔗

class User
{
	private string _name;
	private int _age;
	public string Name {
	get {return _name;} 
	set {_name =value;}}
	public int Age {
	get {return _age;} }
	public User(string name
	,int age)
	{
	_name =name;
	_age = age;
	}
	public void DisplayInfo( 
	params int[]  ar)
	{
		foreach(var item in ar)
		{
			Console.WriteLine(item);    
		}
		Console.WriteLine(
		$"Name: {Name}  Age: {Age}");
	}
}
User tom = new User("Serg",44);
tom.DisplayInfo(1,2,3,4);