人没有目标,
就容易得抑郁症.
而目标太多,
就容易得焦虑症.
我认为人生 ,
本没有什么意义.
但一定要自己努力 ,赋予一个意义.
因为有了意义 ,就有了目标 ,
而目标催生干劲 ,干劲促使学习 ,
学习助人成长 ,成长使人坚毅 ,
而坚毅使我们创造奇迹.
如果你实在找不到人生的意义 ,
那么提升心性 ,磨练灵魂 ,就是你最好的人生意义.
日期:2024年3月27日
1. 创建 dotnet 项目
- 生成控制台项目 dotnet new console -o XxxxProject
- 在生成的目录中创建解决方案 dotnet new sln
- 将解决方案添加到项目中 dotnet sln add XxxxProject/XxxxProject.csproj
- 运行 dotnet 项目 dotnet run
2. 其他类型转换为枚举类型
class Program
{
static void Main(string[] args)
{
string girl = "YEN";
Gender gender_girl = (Gender)Enum.Parse(typeof(Gender), girl);
Console.WriteLine(gender_girl);
}
public enum Gender
{
YAN,
YEN
}
}
3. out,ref,params 参数
out 参数要求在方法的内部必须为其赋值
class Program
{
static void Main(string[] args)
{
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10 };
double average;
// out 参数 在函数调用时可以不赋值
int[] result = GetNumber(numbers, out average);
Console.WriteLine("最大值为 {0},最小值为 {1} 总和为 {2}, 平均值为 {3:0.00}", result[0], result[1], result[2], average);
}
public static int[] GetNumber(int[] nums, out double average)
{
int[] res = [nums.Max(), nums.Min(), nums.Sum()];
// out 参数必须在函数内部为其赋值
average = nums.Average();
return res;
}
}
out 参数使用案例
class Program
{
static void Main(string[] args)
{
int num;
bool result = TryParse("123abc", out num);
Console.WriteLine("是否转换成功 {0} 转换后的值为 {1}", result, num);
}
/// <summary>
/// 实现自己的TryParse函数
/// </summary>
/// <param name="str">要转换成int类型的字符串</param>
/// <param name="num">转换后的int类型的数值</param>
/// <returns>转换是否成功</returns>
public static bool TryParse(string str, out int num)
{
try
{
num = Convert.ToInt32(str);
return true;
}
catch
{
num = 0;
return false;
}
}
}
ref 参数的使用
ref 参数在使用之前必须初始化
class Program
{
static void Main(string[] args)
{
// 将一个变量带入一个方法中进行改变,改变完成之后,再将改变后的值带出方法。
int salary = 100;
Console.WriteLine("改变以前的值{0}", salary);
Add(100, ref salary);
Console.WriteLine("改变以后的值{0}", salary);
}
public static void Add(int num, ref int num2)
{
num2 += num;
}
}
params 可变参数
- params 将后续类型一样的参数 处理成数组
- params 必须是形参参数的最后一位
- params 参数的数据类型必须一致
class Program
{
static void Main(string[] args)
{
int[] score = { 100, 99, 88, 77, 66 };
ScoreInfo("YAN", 100, 99, 88, 77, 66, 69);
ScoreInfo("YEN",score);
ScoreInfo("YAN-YEN",100, 99, 88, 77, 66,10001); // error
// void ScroeInfo(string name, int id,params int[] score);
}
public static void ScoreInfo(string name, params int[] score)
{
Console.WriteLine("{0} 这次考试的成绩为 {1}", name, score.Sum());
}
}
4. 方法重载
方法的重载指的是方法的名称相同,但参数不同--包含参数个数和参数类型且和返回值没有关系
class Program
{
static void Main(string[] args)
{
Console.WriteLine("M {0}", M(35.5, 33.5));
Console.WriteLine("M(\"YAN\",\"YEN\") {0}", M("YAN", "YEN"));
Console.WriteLine("M(\"YAN\",\"--\",\"YEN\") {0}", M("YAN", "--", "YEN"));
}
public static double M(double d1, double d2)
{
return d1 + d2;
}
public static int M(int int1, int int2)
{
return int1 + int2;
}
public static string M(string str1, string str2)
{
return str1 + str2;
}
public static string M(string str1, string str2, string str3)
{
return str1 + str2 + str3;
}
}
5. 方法的递归
class Program
{
public static int i = 0;
static void Main(string[] args)
{
TellStory();
}
public static void TellStory()
{
Console.WriteLine("从前有座山");
Console.WriteLine("山里有座庙");
Console.WriteLine("庙里有个老和尚和小和尚");
Console.WriteLine("有一天,小和尚哭了,老和尚给小和尚讲了一个故事");
i++;
if (i >= 10)
{
return;
}
TellStory();
}
}