A
A
Artyom2018-11-14 19:28:36
C++ / C#
Artyom, 2018-11-14 19:28:36

How to write c# code when using classes?

I have a class that has 2 properties that accept integer values. In the main code flow, I write a loop through which I write n class instances to list. Next, let's say I need to sort these instances by some field. And how should I arrange it? Create a separate class for sorting? Is it simple to write this sorting in the main code flow (progtam)? Or in the main code flow to issue it as a procedure? And the same with all other things, inputs / outputs, etc. How is it all arranged? In separate procedures, or too classes?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
shai_hulud, 2018-11-14
@shai_hulud

There will be ten options for solving your problem. All of them will be different and solve the problem. For example:
1) Writing a comparator class and sorting with it
2) Creating an instance of a comparator from a reference to a comparison method
3) Creating an instance of a comparator from a comparison lambda
4) Passing a reference to a comparison method to the Sort method of the list
5) Passing a comparison lambda to the Sort method list
6) Implementation of IComparable and call Sort without parameters
7) Implement one of the sorting algorithms in a separate method and call this method
8) Implement one of the sorting algorithms in a separate class with code spacing into separate class methods
9) Sort with a bubble directly in Main
10) Put everything into the database through a convenient ORM and select with the right order to get it in the right order
And which one to choose depends on a) the amount of data to be sorted b) the available development time c) memory and CPU budgets d) the style of writing code in the company e) personal preferences and knowledge.
Which one is "correct"? All. Which to choose? As experience suggests, there will be experience and with it will come an understanding of what choice to make.

V
Vyacheslav Rychkov, 2018-11-14
@rychkov_v_a

It can be done differently, for example: c-sharp.pro/?p=1192

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question