public class Program
{
public static void Main(string[] args)
{
var c = check.myValue("Example 1"); //This is the pattern I've to use, don't want to create an object (Is it possible to use it with static class)
Console.WriteLine(c.result1);
Console.WriteLine(c.result2);
}
}
public static class check
{
public static void myValue(string qr)
{
public string result1 = "My Name" + qr;
public string result1 = "You're" + qr;
}
}
Run Code Online (Sandbox Code Playgroud)
在这里看到在线示例(代码不起作用)
main函数的每一件事我都要使用完全相同的模式,因为我会在很多不同的类中使用它,而且我不想每次都使用非静态类创建对象.
如果我错了,请纠正我
该代码的语法有很多问题,@ Sergey在他的回答中提到了这个问题.
您似乎想要从静态方法返回类的实例,并且该类应包含两个属性.
您可以通过创建包含属性的实际非静态类来实现:
public class Check
{
public string Result1 { get; set; }
public string Result2 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后从静态方法返回一个新实例:
public static Check MyValue(string qr)
{
var result = new Check();
result.Result1 = "My Name" + qr;
result.Result2 = "You're" + qr;
return result;
}
Run Code Online (Sandbox Code Playgroud)
但是,您在代码中的注释中说您不想使用对象.
在这种情况下,您似乎想要使用静态属性.这通常不值得推荐,但它看起来像这样:
public static class Check
{
public static string Result1 { get; set; }
public static string Result2 { get; set; }
public static void MyValue(string qr)
{
Result1 = "My Name" + qr;
Result2 = "You're" + qr;
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以Check.Result1在调用方法后阅读MyValue().
| 归档时间: |
|
| 查看次数: |
65 次 |
| 最近记录: |