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 ...
随机推荐
- #2069:Coin Change(完全背包)
Problem Description Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cen ...
- Spring注解@Resource和@Autowired区别对比 (附 Maven 引入方法)
@Resource 导入方法: <dependency> <groupId>javax.annotation</groupId> <artifactId> ...
- AtCoder Beginner Contest 168 (A~E,E题很有意思)
比赛链接:Here AB水题, C - : (Colon) 时针转过得角度为:\(2π \times \frac{h + \frac m{12}}{12}\) 分针转过得角度为:\(2π \times ...
- C++跨DLL内存所有权问题探幽(一)DLL提供的全局单例模式
最近在开发的时候,特别是遇到关于跨DLL申请对象.指针.内存等问题的时候遇到了这么一个问题. 问题 跨DLL能不能调用到DLL中提供的单例? 问题比较简单,就是我现在有一个进程A,有DLL B DLL ...
- Canvas原生绘制树状结构拓扑图
其实当前Web库实现Canvas绘制树状结构的组件很多,而且功能也很强大,但是难免有些场景无法实现需要自己开发,本文主要是提供一种思路 先附一个不错的拓扑图开发地址:https://www.zhihu ...
- freeswitch的任务引擎问题与解决方案
概述 freeswitch核心框架中有一个定时任务系统task,在开发过程中用来做一些延时操作和异步操作很方便. 我们在VOIP的呼叫流程中,经常会有一些对实时性要求没那么高的操作,或者会有阻塞流程的 ...
- C# Emit动态生成代码
Emit(动态方法生成)技术是一种在运行时动态生成和执行代码的技术.它的产生背景可以追溯到早期的.NET Framework版本. 在早期的.NET Framework中,开发人员通常使用反射来动态创 ...
- 解决 idea maven plugins 报红波浪线
导入新项目到 idea 的时候,由于依赖的环境以及项目中 maven 编译所依赖的pom的版本不同,很多时候导入到idea的时候 maven Plugins 会出现报红的情况,这是由于maven仓库中 ...
- SpringBoot中使用LocalDateTime踩坑记录
.markdown-body { line-height: 1.75; font-weight: 400; font-size: 16px; overflow-x: hidden; color: rg ...
- C:\Keil_v5\ARM\ARMCC\include\stdint.h contains an incorrect path.
1.问题 在使用Keil uvison5打开例程代码进行学习时,发现部分.h文件无法读取 2.解决方法 1.找到如图的设置按钮(小锤子) 2.根据自己所用的是C/C++还是ARM选择(我这里是C/C+ ...