- Регистрация
- 1 Мар 2015
- Сообщения
- 1,481
- Баллы
- 155
I'm working on a C# console application and would love some feedback. Specifically:
What's the best way to optimize performance when counting repeated items in a list?
How can I improve resource management (e.g., file handling)?
Any general tips to improve the readability and maintainability of my code?
Thanks in advance for any advice! ?
using System.Linq.Expressions;
namespace muz_app
{
internal class muzeum
{
class Muzeum
{
public string teremaz { get; set; }
public string muveszneve { get; set; }
public string alkotascime { get; set; }
public string tipusa { get; set; }
}
static List<Muzeum> muzeumok = new List<Muzeum>();
static void Main(string[] args)
{
void beolvasas()
{
StreamReader sr = new StreamReader("muzeum.csv");
sr.ReadLine();
while (!sr.EndOfStream)
{
string[] rekord = sr.ReadLine().Split(";");
Muzeum m = new Muzeum();
m.teremaz = rekord[0];
m.muveszneve = rekord[1];
m.alkotascime = rekord[2];
m.tipusa = rekord[3];
muzeumok.Add(m);
}
sr.Close();
}
void negyeik()
{
HashSet<string> termek = new HashSet<string>();
foreach (var item in muzeumok)
{
termek.Add(item.teremaz);
}
Console.WriteLine($"4. feladat Összesen {termek.Count} darab terem van.");
}
void otodik()
{
HashSet<string> muveszek = new HashSet<string>();
Dictionary<string, int> szotar = new Dictionary<string, int>();
foreach (var item in muzeumok)
{
muveszek.Add(item.muveszneve);
}
foreach (var item in muveszek)
{
int szamlalo = 0;
for (int i = 0; i < muzeumok.Count; i++)
{
if (item == muzeumok.muveszneve)
{
szamlalo++;
}
}
szotar.Add(item, szamlalo);
}
Console.WriteLine("5. feladat:");
foreach (var item in szotar)
{
Console.WriteLine($"{item.Key} : {item.Value}");
}
}
beolvasas();
negyeik();
otodik();
}
}
}
What's the best way to optimize performance when counting repeated items in a list?
How can I improve resource management (e.g., file handling)?
Any general tips to improve the readability and maintainability of my code?
Thanks in advance for any advice! ?
using System.Linq.Expressions;
namespace muz_app
{
internal class muzeum
{
class Muzeum
{
public string teremaz { get; set; }
public string muveszneve { get; set; }
public string alkotascime { get; set; }
public string tipusa { get; set; }
}
static List<Muzeum> muzeumok = new List<Muzeum>();
static void Main(string[] args)
{
void beolvasas()
{
StreamReader sr = new StreamReader("muzeum.csv");
sr.ReadLine();
while (!sr.EndOfStream)
{
string[] rekord = sr.ReadLine().Split(";");
Muzeum m = new Muzeum();
m.teremaz = rekord[0];
m.muveszneve = rekord[1];
m.alkotascime = rekord[2];
m.tipusa = rekord[3];
muzeumok.Add(m);
}
sr.Close();
}
void negyeik()
{
HashSet<string> termek = new HashSet<string>();
foreach (var item in muzeumok)
{
termek.Add(item.teremaz);
}
Console.WriteLine($"4. feladat Összesen {termek.Count} darab terem van.");
}
void otodik()
{
HashSet<string> muveszek = new HashSet<string>();
Dictionary<string, int> szotar = new Dictionary<string, int>();
foreach (var item in muzeumok)
{
muveszek.Add(item.muveszneve);
}
foreach (var item in muveszek)
{
int szamlalo = 0;
for (int i = 0; i < muzeumok.Count; i++)
{
if (item == muzeumok.muveszneve)
{
szamlalo++;
}
}
szotar.Add(item, szamlalo);
}
Console.WriteLine("5. feladat:");
foreach (var item in szotar)
{
Console.WriteLine($"{item.Key} : {item.Value}");
}
}
beolvasas();
negyeik();
otodik();
}
}
}