我有一个包含Main
方法和一个函数的控制台应用程序。
如何从Main
方法中调用该函数?
我知道下面的代码是行不通的:
static void Main(string[] args)
{
string btchid = GetCommandLine();// GetCommandline is a mthod which returns a string
}
我有一个包含Main
方法和一个函数的控制台应用程序。
如何从Main
方法中调用该函数?
我知道下面的代码是行不通的:
static void Main(string[] args)
{
string btchid = GetCommandLine();// GetCommandline is a mthod which returns a string
}
还有一个选项
var p = new Program();
string btchid = p.GetCommandLine();
将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();
}
}
}
static class Program
{
[STAThread]
static void Main()
{
string btchid = Program.GetCommandLine();
}
private static string GetCommandLine()
{
string s = "";
return s;
}
}
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;
}
}
}
类似这样的东西:
[STAThread]
static void Main(string[] args) {
string btchid = GetCommandLine();// GetCommandline is a mthod which returns a string
}
static string GetCommandLine(){
return "Some command line";
}
GetCommandLine
必须是一个静态函数
字符串 btchid = classnamehere.GetCommandLine();
假设GetCommandLine
是静态的。