C# (C Sharp) 是一种通用、面向对象的编程语言,由微软公司开发。它是.NET平台的主要编程语言之一,被设计用于构建各种类型的应用程序,从桌面应用到Web应用和移动应用,甚至是嵌入式系统。C#结合了C和C++的优点,并加入了一些Java的特性,因此在语法和概念上与这些语言有相似之处。
以下是C#的一些特点和重要特性:
- 面向对象:C#是一种面向对象的语言,支持封装、继承和多态等面向对象的概念,使得代码更具可维护性和扩展性。
- 安全性:C#在设计上注重安全性,提供了类型安全、内存管理和异常处理等特性,有助于防止常见的编程错误和安全漏洞。
- 交叉平台:C#代码可以运行在.NET平台上,允许跨多个平台运行,包括Windows、Linux和macOS等。
- 托管语言:C#是一种托管语言,意味着它在运行时依赖于.NET运行时环境(Common Language Runtime - CLR),这提供了自动内存管理和垃圾回收等功能,简化了开发过程。
- 丰富的类库:C#拥有强大的类库,包括.NET Framework类库和.NET Core类库,提供了大量的功能和工具,方便开发者编写各种类型的应用程序。
- 多平台开发:随着.NET Core的推出,C#可以跨平台开发,并支持用于Web开发的ASP.NET Core和移动开发的Xamarin。
- 强类型语言:C#是一种静态类型的语言,需要在编译时声明变量的类型,并且强制类型检查,有助于提高代码的稳定性和安全性。
- LINQ:C#引入了语言集成查询(Language Integrated Query - LINQ)功能,使得对各种数据源进行查询和操作更加简洁和直观。
总体来说,C#是一种功能强大、易学易用的编程语言,适用于多种应用场景,包括桌面应用、Web应用、移动应用、云服务等。它是.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();
}
}
}