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();
}
}
}
C# 笔记之基本语法的更多相关文章
- python笔记之中缀语法和管道实现
python笔记之中缀语法和管道实现 你知道什么是中缀语法吗?你知道python中的中缀操作是什么吗?那你知道操作python也是可以像unix的管道符一样方便吗?那么,废话不说了,直接上代码. cl ...
- django2笔记:路由path语法
django2笔记:路由path语法 9月23,Django 发布了2.0a1版本,这是一个 feature freeze 版本,如果没有什么意外的话,2.0正式版不会再增加新的功能了.按照以往的规律 ...
- python3.4学习笔记(一) 基本语法 python3不向下兼容,有些语法跟python2.x不一样
python3.4学习笔记(一) 基本语法 python3不向下兼容,有些语法跟python2.x不一样,IDLE shell编辑器,快捷键:ALT+p,上一个历史输入内容,ALT+n 下一个历史输入 ...
- Python:笔记(1)——基础语法
Python:笔记(1)——基础语法 我很抱歉有半年没有在博客园写过笔记了,客观因素有一些,但主观原因居多,再多的谴责和批判也都于事无补,我们能做的就是重振旗鼓,继续出发! ——写在Python之前 ...
- C#快速入门笔记(1)——基础语法
C#快速入门笔记(1)——基础语法 总体框架:
- ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现
ASP.NET MVC 学习笔记-2.Razor语法 1. 表达式 表达式必须跟在“@”符号之后, 2. 代码块 代码块必须位于“@{}”中,并且每行代码必须以“: ...
- 基于.net的分布式系统限流组件 C# DataGridView绑定List对象时,利用BindingList来实现增删查改 .net中ThreadPool与Task的认识总结 C# 排序技术研究与对比 基于.net的通用内存缓存模型组件 Scala学习笔记:重要语法特性
基于.net的分布式系统限流组件 在互联网应用中,流量洪峰是常有的事情.在应对流量洪峰时,通用的处理模式一般有排队.限流,这样可以非常直接有效的保护系统,防止系统被打爆.另外,通过限流技术手段,可 ...
- Java学习笔记之---基础语法
Java学习笔记之---基础语法 一. Java中的命名规范 (一)包名 由多个单词组成时,所有字母小写(例如:onetwo) (二)类名和接口 由多个单词组成时,所有单词首字母大写(例如:OneTw ...
- CUBRID学习笔记 41 sql语法之select
cubrid的中sql查询语法 SELECT [ ] [{TO | INTO} ][FROM ] [WHERE ][GROUP BY {col_name | expr} [ASC | DESC], . ...
- Essential C++ 学习笔记01--基本语法
<Essential C++>1.1-1.4节笔记 1. main 函数 main 函数是代码的入口,若无 main 函数,编译不通过. main 函数通常声明为 int, return ...
随机推荐
- Linux 下 Docker 操作遭到守护程序套接字时访问权限被拒绝
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker. ...
- Codeforces Round #645 (Div. 2)
这一次的Div.2 大多数学思维.. A. Park Lightingtime https://codeforces.com/contest/1358/problem/A 题意:给一个n,m为边的矩形 ...
- 题解 CF1550C. Manhattan Subarrays (思维)
来源:Educational Codeforces Round 111 (Rated for Div. 2) 不难但很好的思维题 设 \(d(p,q)\) 为 \(p,q\) 两点之间的曼哈顿距离 给 ...
- [网络克隆]利用tiny proxy实现局域网内批量安装WINDOWS操作系统。
说明:本文支持UEFI及BIOS系统克隆.支持UEFI及BIOS网络引导,适用于新旧几乎所有支持网络启动的电脑. 事前准备: 保证被克隆的电脑为同一配置,或者各硬件差异不大,比如CPU同如英特尔8代的 ...
- java对excle操作:下载、上传以及上传中错误数据动态生成excle给用户下载
工作中经常遇到excle文件的上传下载,这里就总结一下相关的操作,尤其是最后一个方法"上传excle文件校验数据格式,挑出格式错误的数据"网上没有找到相关的例子,自己组合改写了一下 ...
- 你做的 9 件事表明你不是专业的 Python 开发人员
本文转载自国外论坛 medium,原文地址: https://medium.com/navan-tech/7-java-features-you-might-not-have-heard-of-ade ...
- 如何使用chatgpt编写代码
功能列举 回答编程问题 我想让你充当 Stackoverflow 的帖子.我将提出与编程有关的问题,你将回答答案是什么.我希望你只回答给定的答案,在没有足够的细节时写出解释.当我需要用英语告诉你一些事 ...
- C#通过泛型实现对子窗体的不同操作
private void button1_Click(object sender, EventArgs e) { FormOperate<object>();//调用FormOperate ...
- 08-避免Latch的产生
1.Latch简介 Latch就是锁存器,是一种在异步电路系统中,对输入信号电平敏感的单元,用来存储信息 锁存器在数据未锁存时,输出端的信号随输入信号变化,就像信号通过一个缓冲器,一旦锁存信号有效,数 ...
- 如何让Dec-C++支持C++11
1.问题 Dev-C++默认设置中是不支持C++11版本特性的,如Lambda表达式,nullptr等均不提供支持 2.解决 设置编译选项 编译时加上命令-std==c++11即可