w3cschool通过C语言类比快速上手C#
Posted 小哈里
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了w3cschool通过C语言类比快速上手C#相关的知识,希望对你有一定的参考价值。
1、环境
- C#编写的应用程序使用.NET Framework及其组件(类似于JRE的运行环境)
- 一般使用Visual Studio创建开发环境,控制台应用程序使用纯文本界面,适合初学者。
2、语法
- C#的板子包括导入using, namespace 套class 套Main函数。
输出是Console.WriteLine或Write,区别是换行符。
输入是yourName = Console.ReadLine();
辅助用Convert.ToInt32和Convert.ToInt64,Convert.ToDouble。
using System;
namespace SoloLearn{
class Program{
static void Main(string[] args){
Console.WriteLine("Hello World!");
Console.WriteLine("x的值是: {0}", x);
double x = Convert.ToDouble(y);
}
}
}
-
数据类型(都跟C一样)
int, float, double, char, bool, string
声明,赋值,使用
多一个var变量:var num = 15.23; 必须赋值才能初始化,赋值后自动选择类型
常量const一样
运算符,+ - * / % , +=, -=, /= , ++, --, 都一样 -
控制流(都跟C一样)
if, else, while, do while, for, switch都一样
continue, break; 都一样
关系运算符 <=, != >=, == 一样
逻辑运算符&&,||, ! 都一样
三目运算符?: -
方法(都跟C一样)
函数:声明,参数,返回值都一样。
传参数多一个 int res = Area(w: 5, h: 8);指定变量值.
传递引用参数的时候static void Sqr(ref int x).,相当于int &x
递归,重载,都一样。 -
类和对象(都跟C一样)
class跟c一样, 实例化的时候Dog bob = new Dog();
访问器get { return name; },set { name = value; }可以赋值和返回值
为了能够让Main方法直接调用,声明的方法必须是static
派生一个类class Dog : Animal {
抽象类abstract class Shape {
创建一个接口public interface IShape { -
数组和字符串(都跟C一样)
数组声明变了int[ ] myArray = new int[5];
string[ ] names = new string[ ] {“John”, “Mary”, “Jessica”};
多出一种循环
foreach (int x in arr) {
sum += x;
}
多维数组int[ , ] x = new int[3,4];
IndexOf(value):返回索引值在字符串中第一次出现的位置
Remove(index):删除字符串中指定位置后面的所有字符。
Replace(oldValue, newValue): 替换字符串中的指定值。
Substring(index, length):从指定的索引开始,返回指定长度的子串。 如果未指定长度,则该操作将持续到字符串的末尾。
以上是关于w3cschool通过C语言类比快速上手C#的主要内容,如果未能解决你的问题,请参考以下文章