I am new to C# and have a difficulty with the code structure. My application has:

  1. GUI with button Show my Ethernet IP. When user clicks this button PC's IP address is displayed.

  2. Class ModbusTCPSlaveMachine. Within this class I create TCPListenter and supply it with PC's Ethernet IP address.

In both cases I get local IP address using the same method GetAllLocalIPv4. Right now I have 2 copies of this method in the code (in classes Form and ModbusTCPSlaveMachine) which is not good I guess. For example, in Python I would simply place the method in some utility module. I am aware that methods cannot be located directly in the namespace in C# while don't quite understand why.

The option I can think of is to place this function in a separate class - some utility class containing a single method (based on Python experience). Not sure if this is reasonable solution in C# beacuse this utility class looks like a meaningless container to me.

How can/should I solve the problem?

up vote 7 down vote accepted

Just using a static method is the way to go here. If your function “belongs” to some existing class you can declare it as a public static method there. If it should be separate, you can put the method into a static class. A C# static class has no constructor and cannot be instantiated, and is therefore suitable as a namespace for functions.

As an example in the standard library, consider the Math class.

  • Thank you for the answer. I'll try to look for the place in the code where the function (and IP address itself) may belong to. – Poolka Nov 29 at 6:22
  • The Math class is a set of deterministic functions. Having them as static methods makes complete sense. A method that fetches the current IP address of a machine is not deterministic though: it will return different results depending on the IP address at that time, whether a network connection exists etc. Making such a method static will then lead to the code being extremely difficult to test. So "just using a static method is the way to go here" is bad advice. It should be an instance method accessed via an interface. – David Arno Nov 29 at 12:47

Placing a method in a normal class has these benifits:

  1. You can put related methods in the same class and they can be moved around together.

  2. You can initialize object state at an earlier time then when the method is called. This gives you the same benefits a closure has.

  3. You can override the method with a different method without changing the clients that use it.

If none of that will ever mater to you then static saves you some keyboard typing.

Are you sure you don't want to put this as a normal member function in its own class, maybe called NetworkInfo? Then you could mock it in test cases and have predictable results that don't depend on the machine the code is running on.

  • Thank you for the reply. Originally the function was located in ModbusTCPSlaveMachine class only. I use it to start TCPListener with relevant PC's IP address. Later the button was added. The problem is that application (button) may run without any existing modbus machine. My Python experience tells me - hey, create separate module with usefull functions and call them wherever or whenever you need. This NetworkInfo class gonna be some analogue of Python's module? Static class like @amon advised or normal class instantiated with the GUI? – Poolka Nov 29 at 10:46

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.