using System;
public class Matrix
{
private int[,] _data;
// Конструктор: инициализация матрицы заданного размера
public Matrix(int rows, int columns)
{
_data = new int[rows, columns];
}
// Индексатор для доступа к элементам матрицы
public int this[int row, int column]
{
get { return _data[row, column]; }
set { _data[row, column] = value; }
}
}
public class Program
{
public static void Main()
{
Matrix matrix = new Matrix(3, 3);
// Заполнение матрицы данными
int count = 1;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
matrix[i, j] = count++;
Console.WriteLine("Matrix elements:");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
}
}
using System;
public class Temperature
{
public double Celsius { get; set; }
public Temperature(double celsius)
{
Celsius = celsius;
}
// Неявное преобразование из double в Temperature
public static implicit operator Temperature(double celsius)
{
return new Temperature(celsius);
}
// Явное преобразование из Temperature в double
public static explicit operator double(Temperature temperature)
{
return temperature.Celsius;
}
}
public class Program
{
public static void Main()
{
// Неявное преобразование
Temperature t1 = 25.0;
// Явное преобразование
double celsiusValue = (double)t1;
Console.WriteLine($"Temperature in Celsius: {t1.Celsius}");
Console.WriteLine($"Celsius value as double: {celsiusValue}");
}
}
public class Fraction
{
public int Numerator { get; set; }
public int Denominator { get; set; }
public Fraction(int numerator, int denominator)
{
Numerator = numerator;
Denominator = denominator;
}
// Перегрузка оператора +
public static Fraction operator +(Fraction a, Fraction b)
{
int commonDenominator = a.Denominator * b.Denominator;
int numerator = (a.Numerator * b.Denominator) + (b.Numerator * a.Denominator);
return new Fraction(numerator, commonDenominator);
}
// Перегрузка оператора *
public static Fraction operator *(Fraction a, Fraction b)
{
int numerator = a.Numerator * b.Numerator;
int denominator = a.Denominator * b.Denominator;
return new Fraction(numerator, denominator);
}
}
public class Point
{
public int X { get; set; }
public int Y { get; set; }
public Point(int x, int y)
{
X = x;
Y = y;
}
// Перегрузка оператора равенства (==)
public static bool operator ==(Point a, Point b)
{
// Проверка на null, если это необходимо
return a.X == b.X && a.Y == b.Y;
}
// Перегрузка оператора неравенства (!=)
public static bool operator !=(Point a, Point b)
{
return !(a == b);
}
}
public class Vector
{
public double X { get; set; }
public double Y { get; set; }
public Vector(double x, double y)
{
X = x;
Y = y;
}
// Перегрузка унарного оператора - (отрицание)
public static Vector operator -(Vector a)
{
return new Vector(-a.X, -a.Y);
}
}