C# 笔记之基本语法
C#是一种现代的、通用的编程语言,由微软公司开发和推广。它于2000年发布,是一种结构化、面向对象和组件化的语言,旨在为Windows操作系统和Microsoft .NET框架提供强大的支持。可用于开发各种类型的应用程序。它在企业和开源社区中都非常受欢迎,并且具有广泛的支持和资源。
标准输入输出:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hello world");
string Str = Console.ReadLine();
Console.WriteLine(Str);
}
}
}
常用变量类型:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
byte s_byte = 254;
sbyte s_sbyte = 126;
short s_short = 32766;
int s_int = 2147483645;
double s_double = 3.1415926;
decimal d_decimal = 5000m;
string s_string = "hello lyshark";
}
}
}
if语句:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
const int x = 100;
const int y = 200;
int source = 0;
Console.WriteLine("请输入一个数:");
source = int.Parse(Console.ReadLine());
if (source == 0)
{
Console.WriteLine("你没有输入任何数.");
}
else if (source > x && source < y)
{
Console.WriteLine("符合规范");
}
Console.ReadKey();
}
}
}
switch
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("输入一个整数: ");
int score = Convert.ToInt32(Console.ReadLine());
switch (score / 10)
{
case 10:
case 9:
Console.WriteLine("A");break;
case 8:
case 7:
Console.WriteLine("B");break;
default:
Console.WriteLine("none");break;
}
Console.ReadKey();
}
}
}
while-dowhile
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] MyArray = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int index = 0;
while (index < 10)
{
Console.WriteLine("数组中的值: {0}", MyArray[index]);
index++;
}
index = 0;
do
{
Console.Write("{0} ", MyArray[index]);
index++;
} while (index < 10);
Console.ReadKey();
}
}
}
for
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] MyArray = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
for (int index = 0; index < MyArray.Length; index++)
{
Console.WriteLine("下标:{0} --> 数据: {1}", index, MyArray[index]);
}
ArrayList alt = new ArrayList();
alt.Add("你好");
alt.Add("世界");
foreach (string Str in alt)
{
Console.WriteLine("输出: {0}", Str);
}
Console.ReadKey();
}
}
}
break
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
for (int x = 0; x < 5; x++)
{
for(int y=0; y<10 ;y++)
{
if (y == 3)
break;
Console.WriteLine("输出: {0}",y);
}
}
Console.ReadKey();
}
}
}
goto
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] MyStr = new string[3];
MyStr[0] = "hello";
MyStr[1] = "world";
MyStr[2] = "lyshark";
for (int x = 0; x < MyStr.Length; x++)
{
if(MyStr[x].Equals("lyshark"))
{
goto Is_Found;
}
}
Is_Found:
Console.Write("查找到成员");
Console.ReadKey();
}
}
}
判断闰年案例:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
Console.Write("输入年份: ");
int year = Convert.ToInt32(Console.ReadLine());
Console.Write("输入月份: ");
int month = Convert.ToInt32(Console.ReadLine());
if (month >= 1 && month <= 12)
{
int day = 0;
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: day = 31; break;
case 2:
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
day = 29;
else
day = 28;
break;
default: day = 30; break;
}
Console.WriteLine("{0}年{1}月有{2}天", year, month, day);
}
}
catch
{
Console.WriteLine("异常了");
}
Console.ReadKey();
}
}
}
99口诀表
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
for (int x = 1; x <= 9; x++)
{
for (int y = 1; y <= x; y++)
{
Console.Write("{0} * {1} = {2} \t", y, x, x * y);
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
随机数产生:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Random rand = new Random();
for (int x = 0; x < 100; x++)
{
int Num = rand.Next(1, 1024);
Console.WriteLine("随机数: {0}", Num);
}
Console.ReadKey();
}
}
}
枚举类型:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
public enum QQState
{
OnLine = 1,
OffLine,
Leave,
Busy,
QMe
}
class Program
{
static void Main(string[] args)
{
QQState state = QQState.OnLine;
// 将state强转为整数
int num = (int)state;
Console.WriteLine(num);
// 将整数强转为枚举类型
int num1 = 2;
QQState x = (QQState)num1;
Console.WriteLine("状态: {0}",x);
// 输入一个号兵输出对应状态
string input = Console.ReadLine();
switch(input)
{
case "1":
QQState s1 = (QQState)Enum.Parse(typeof(QQState), input);
Console.WriteLine("状态: {0}", s1);
break;
}
Console.ReadKey();
}
}
}
定义结构数据:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
// 定义结构数据
public struct Person
{
public double width;
public double height;
// 结构体支持构造函数
public Person(double x, double y)
{
width = x;
height = y;
}
}
static void Main(string[] args)
{
Person per;
per.width = 100;
per.height = 200;
Console.WriteLine("x = {0} y = {1}", per.width, per.height);
Person ptr = new Person(10, 20);
Console.WriteLine("x = {0} y = {1}", ptr.width, ptr.height);
Console.ReadKey();
}
}
}
文章出处:https://www.cnblogs.com/LyShark/p/13156473.html
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?