在同一个项目的两个不同文件中定义和调用函数,
将一个double类型的数转化为不保留小数的字符串
面向C#初学者
文件一
// Program.csusing System;
using MyApp; // 引入包含 Functions 类的命名空间namespace MyApp
{class Program{static void Main(string[] args){// 调用 Greet 函数string name = "Alice";//Functions是类,Great是类下的一个方法,这个方法需要输入一个字符串,会返回一个字符串Console.WriteLine(Functions.Greet(name)); // 输出: Hello, Alice!//调用double转化为不保留小数的字符串double num3 = 3456.123;Console.WriteLine(Functions.DoubleToString(num3));// 保持控制台窗口打开以查看输出(如果你需要的话)Console.ReadLine();}}
}
文件二
// Functions.csusing System;namespace MyApp
{//定义一个静态类public static class Functions{// 定义一个 静态 方法public static string Greet(string name){return $"Hello, {name}!";}public static string DoubleToString(double dou){string str;str = dou.ToString("F0");return str;}}
}
输出结果:
Hello, Alice!
3456