Reputation: 419
I apologize if this is a duplicate, but I haven't been able to find any answers to the issue I am having. I'm working with the following block of code:
IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
Console.WriteLine("Interface information for {0}.{1} ",
computerProperties.HostName, computerProperties.DomainName);
foreach (NetworkInterface adapter in nics)
{
IPInterfaceProperties properties = adapter.GetIPProperties();
Console.WriteLine(adapter.Description);
Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length, '='));
Console.WriteLine(" Interface type ... : {0}", adapter.NetworkInterfaceType);
Console.WriteLine(" Physical Address ........... : {0}",
adapter.GetPhysicalAddress().ToString());
Console.WriteLine(" Is receive only.............. : {0}", adapter.IsReceiveOnly);
Console.WriteLine(" Multicast......... : {0}", adapter.SupportsMulticast);
Console.WriteLine();
}
When I run without debugging, and this code is within the Main() method, the Console.WriteLine()
prints the output. However, when I put this into a public static void
class, the output is blank. Can anyone explain why this happens and what I should do instead. I'm still learning C# so I'm sure this is a beginner's mistake. Any help would be appreciated.
Upvotes: 0
Views: 2485
Reputation: 3085
"Main" is a special method in .NET environment. It is a "entry point" of every .NET program.
When yours application/program run, Main will be invoke and every command in this method will be execute.
If you put your code into other method or class and you don't call it inside Main, obviously nothing happen. So if you want run other method, you must call it inside Main.
Hope it useful.
Upvotes: 0
Reputation: 332
I'd guess that the code in your other class is never executed. You don't need the class to be public static and void (not sure if that's possible), but you want the method you're using to be public static void.
This is what you want your other class to look like:
public class OtherClass {
public static void Method() {
// Console.WriteLine code here
}
}
And then your main should call that Method:
public class OriginalClass {
public static void Main(String[] args) {
OtherClass.Method();
}
}
If you meant other method/function instead of class, it will look like this:
public class OriginalClass {
public static void Main(String[] args) {
Method();
}
public static void Method() {
// Console.WriteLine code here
}
}
Upvotes: 1