How do you call a method from static main ()?

I have a console application with the Main method and function.

How can I call a function call from the Main method?

I know the code below does not work

 static void Main(string[] args) { string btchid = GetCommandLine();// GetCommandline is a mthod which returns a string } 
+6
source share
8 answers

There also

 var p = new Program(); string btchid = p.GetCommandLine(); 
+13
source

Make GetCommandLine static !

 namespace Lab { public static class Program { static string GetCommandLine() { return "Hellow World!"; } static void Main(string[] args) { System.Console.WriteLine(GetCommandLine()); System.Console.ReadKey(); } } } 
+9
source

You can change the function as static and call it. That's all.

+1
source

GetCommandLine must be a static function

0
source

string btchid = classnamehere.GetCommandLine(); Assuming GetCommandLine is Static

0
source

Something like that:

 [STAThread] static void Main(string[] args) { string btchid = GetCommandLine();// GetCommandline is a mthod which returns a string } static string GetCommandLine(){ return "Some command line"; } 
0
source
 static class Program { [STAThread] static void Main() { string btchid = Program.GetCommandLine(); } private static string GetCommandLine() { string s = ""; return s; } } 
0
source

Linear search approach to your problem:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LinearSearch { class Program { static void Main(string[] args) { int var1 = 50; int[] arr; arr = new int[10]{10,20,30,40,50,60,70,80,90,100}; int retval = linearsearch(arr,var1); if (retval >= 1) { Console.WriteLine(retval); Console.Read(); } else { Console.WriteLine("Not found"); Console.Read(); } } static int linearsearch(int[] arr, int var1) { int pos = 0; int posfound = 0; foreach (var item in arr) { pos = pos + 1; if (item == var1) { posfound = pos; if (posfound >= 1) break; } } return posfound; } } } 
0
source

Source: https://habr.com/ru/post/886305/


All Articles