Доступ к массиву другого класса - C#
Формулировка задачи:
В 1 классе есть матрица. Я хочу сделать копию данной матрицы во 2й класс. Как это сдлеать правильно?
1й класс
getMatrix это свойство, с помощью которого я пытаюсь получить матрицу.
public class Graph
{
//разный код
private List<List<int>> _matrix;//массив для хранения матрицы
//еще код
public List<List<int>> getMatrix
{
get { return _matrix; }
}
}Решение задачи: «Доступ к массиву другого класса»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication6 {
class Program {
static void Main(string[] args) {
Graph graph = new Graph();
List<List<int>> someMatrix = graph.GetMatrix;
Console.WriteLine("Elements in matrix in class: {0}", graph.GetMatrix.Count);
Console.WriteLine("Elements in matrix in Main(): {0}", someMatrix.Count);
graph.FillMatrix();
Console.WriteLine("Elements in matrix in class: {0}",graph.GetMatrix.Count);
Console.WriteLine("Elements in matrix in Main(): {0}",someMatrix.Count);
Console.ReadLine();
}
}
class Graph{
List<List<int>> matrix = new List<List<int>>();
public List<List<int>> GetMatrix {
get {
List<List<int>> temp = new List<List<int>>();
foreach (List<int> t in matrix) {
List<int> intTemp = new List<int>();
intTemp.AddRange(t);
temp.Add(intTemp);
}
return temp;
}
}
public void FillMatrix() {
matrix.Add(new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
}
}
}