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# 笔记之基本语法的更多相关文章

  1. python笔记之中缀语法和管道实现

    python笔记之中缀语法和管道实现 你知道什么是中缀语法吗?你知道python中的中缀操作是什么吗?那你知道操作python也是可以像unix的管道符一样方便吗?那么,废话不说了,直接上代码. cl ...

  2. django2笔记:路由path语法

    django2笔记:路由path语法 9月23,Django 发布了2.0a1版本,这是一个 feature freeze 版本,如果没有什么意外的话,2.0正式版不会再增加新的功能了.按照以往的规律 ...

  3. python3.4学习笔记(一) 基本语法 python3不向下兼容,有些语法跟python2.x不一样

    python3.4学习笔记(一) 基本语法 python3不向下兼容,有些语法跟python2.x不一样,IDLE shell编辑器,快捷键:ALT+p,上一个历史输入内容,ALT+n 下一个历史输入 ...

  4. Python:笔记(1)——基础语法

    Python:笔记(1)——基础语法 我很抱歉有半年没有在博客园写过笔记了,客观因素有一些,但主观原因居多,再多的谴责和批判也都于事无补,我们能做的就是重振旗鼓,继续出发! ——写在Python之前 ...

  5. C#快速入门笔记(1)——基础语法

    C#快速入门笔记(1)——基础语法 总体框架:

  6. ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现

    ASP.NET MVC 学习笔记-2.Razor语法   1.         表达式 表达式必须跟在“@”符号之后, 2.         代码块 代码块必须位于“@{}”中,并且每行代码必须以“: ...

  7. 基于.net的分布式系统限流组件 C# DataGridView绑定List对象时,利用BindingList来实现增删查改 .net中ThreadPool与Task的认识总结 C# 排序技术研究与对比 基于.net的通用内存缓存模型组件 Scala学习笔记:重要语法特性

    基于.net的分布式系统限流组件   在互联网应用中,流量洪峰是常有的事情.在应对流量洪峰时,通用的处理模式一般有排队.限流,这样可以非常直接有效的保护系统,防止系统被打爆.另外,通过限流技术手段,可 ...

  8. Java学习笔记之---基础语法

    Java学习笔记之---基础语法 一. Java中的命名规范 (一)包名 由多个单词组成时,所有字母小写(例如:onetwo) (二)类名和接口 由多个单词组成时,所有单词首字母大写(例如:OneTw ...

  9. CUBRID学习笔记 41 sql语法之select

    cubrid的中sql查询语法 SELECT [ ] [{TO | INTO} ][FROM ] [WHERE ][GROUP BY {col_name | expr} [ASC | DESC], . ...

  10. Essential C++ 学习笔记01--基本语法

    <Essential C++>1.1-1.4节笔记 1. main 函数 main 函数是代码的入口,若无 main 函数,编译不通过. main 函数通常声明为 int, return ...

随机推荐

  1. 一文聊透 IP 地址的那些事

    IP 地址,是一个大家都耳熟能详的名词.以生活举例,IP 在互联网中的作用就像是寄件时的收件人地址和寄件人地址,收件人地址让信件可以被正确送达,寄件人地址则让收到信的人可以回信. IP 地址作为每一个 ...

  2. CDS 重命名失败

    当创建CDS视图,名称命名错误,后将视图名称更改后,激活报错(例如,第一次创建的视图名称为ZVWM014,后改为ZVMM014) SQL view ZVWM014 cannot be renamed ...

  3. SELinux 入门 pt.2

    哈喽大家好,我是咸鱼 在<SELinux 入门 pt.1>中,咸鱼向各位小伙伴介绍了 SELinux 所使用的 MAC 模型.以及几个重要的概念(主体.目标.策略.安全上下文) 我们还讲到 ...

  4. #2069:Coin Change(完全背包)

    Problem Description Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cen ...

  5. POJ3414 Pots( BFS搜索)

    题目: 给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i) 将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DROP(i) 将第i个容器抽干 POUR(i,j) 将第i个 ...

  6. 【Vue CLI】手把手教你撸插件

    本文首发于 vivo互联网技术 微信公众号链接:https://mp.weixin.qq.com/s/Rl8XLUX7isjXNUmbw0-wow作者:ZhuPing 现如今 Vue 作为主流的前端框 ...

  7. centos7.9 安装oracle11g

    安装环境: 操作系统:CentOS Linux release 7.9.2009 (Core)orcle安装包:linux.x64_11gR2_database_1of2.zip. linux.x64 ...

  8. CPLEX通过Python API获取Gap值的方法

    写在前面 最近在使用Cplex求解模型,尽管Cplex的Python API会自动输出引擎日志,但在多次求解中一次次看引擎日志找Gap值并做实验记录很麻烦,所以需要找到获取Gap值的方法.然而我在Cp ...

  9. flexible+rem移动端适配

  10. Nacos源码 (7) Nacos与Spring

    SpringCloud工程可以使用Nacos作为注册中心和配置中心,配置和使用非常简单,本文将简单介绍使用方式,并分析其实现方式. SpringCloud工程集成Nacos SpringCloud工程 ...