Structures C#

Mar 12, 2023 00:00 ยท 145 words ยท 1 minute read

Structure description, tuples, enums

Enums ๐Ÿ”—

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

Tuples ๐Ÿ”—

(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;
}

Structures ๐Ÿ”—

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();

Classes ๐Ÿ”—

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);