c# - 我们如何将字符串从一个方法返回到主方法?
问题描述
我正在尝试PrintBanner
从main
. 但是,它不会让我。
static void Main(string[] args)
{
string banner;
banner = PrintBanner("This is whats supposed to be printed.");
}
public static void PrintBanner()
{
return PrintBanner(banner);
}
我需要从 main 调用消息。但是错误表明没有重载 forPrintBanner
需要一个参数。并且该名称banner
不存在于PrintBanner
.
我应该string banner
输入PrintBanner
方法吗?
解决方案
我不清楚你想在这里完成什么。尽管您的代码似乎希望使用 PrintBanner 方法打印并分配一个值。
public static void Main(string[] args)
{
string banner;
banner = PrintBanner("This is whats supposed to be printed.");
}
public static string PrintBanner(string text)
{
Console.Write(text);
return text;
}
或者您可能不希望方法本身执行分配?:
public static void Main(string[] args)
{
string banner;
PrintBanner(banner = "This is whats supposed to be printed.");
}
public static void PrintBanner(string text)
{
// The text variable contains "This is whats supposed to be printed." now.
// You can perform whatever operations you want with it within this scope,
// but it won't alter the text the banner variable contains.
}
如果不是,请尝试进一步详细说明您的目标。
推荐阅读
- python - 如何可视化使用networkx创建的邻接矩阵
- reactjs - 标记不适用于 google-map-react
- makefile - Makefile 变量的交互式输入
- gitlab - gitlab 中的自动发行说明
- r - 通过变量从r中的命名向量中提取单词
- python - 连接到 Django 数据库的内部服务器错误
- javascript - 如何在 Angular 8 中启动应用程序时首先加载数据服务
- r - 使用 geom_line 和 geom_point 更改图例符号“a”
- kubernetes - 无法使用 Helm Push Artifactory 插件将 helm 包存档推送到工件
- flutter - 在网格视图的中心底部显示小部件