c# 基础语法

c# 基础语法

基础语法

第一个程序

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World");
        }
    }

数据类型

类型 描述 范围 默认值
bool 布尔值 True 或 False False
byte 8 位无符号整数 0 到 255 0
char 16 位 Unicode 字符 U +0000 到 U +ffff '\0'
decimal 128 位精确的十进制值,28-29 有效位数 (-7.9 x 1028 到 7.9 x 1028) / 100 到 28 0.0M
double 64 位双精度浮点型 (+/-)5.0 x 10-324 到 (+/-)1.7 x 10308 0.0D
float 32 位单精度浮点型 -3.4 x 1038 到 + 3.4 x 1038 0.0F
int 32 位有符号整数类型 -2,147,483,648 到 2,147,483,647 0
long 64 位有符号整数类型 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 0L
sbyte 8 位有符号整数类型 -128 到 127 0
short 16 位有符号整数类型 -32,768 到 32,767 0
uint 32 位无符号整数类型 0 到 4,294,967,295 0
ulong 64 位无符号整数类型 0 到 18,446,744,073,709,551,615 0
ushort 16 位无符号整数类型 0 到 65,535 0

类型转换

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            double d = 5673.74;
            int i;

            // 强制转换 double 为 int
            i = (int)d;
            Console.WriteLine(i);
            Console.ReadKey();
        }
    }
}

C# 类型转换方法

序号 方法 & 描述
1 ToBoolean 如果可能的话,把类型转换为布尔型。
2 ToByte 把类型转换为字节类型。
3 ToChar 如果可能的话,把类型转换为单个 Unicode 字符类型。
4 ToDateTime 把类型(整数或字符串类型)转换为 日期-时间 结构。
5 ToDecimal 把浮点型或整数类型转换为十进制类型。
6 ToDouble 把类型转换为双精度浮点型。
7 ToInt16 把类型转换为 16 位整数类型。
8 ToInt32 把类型转换为 32 位整数类型。
9 ToInt64 把类型转换为 64 位整数类型。
10 ToSbyte 把类型转换为有符号字节类型。
11 ToSingle 把类型转换为小浮点数类型。
12 ToString 把类型转换为字符串类型。
13 ToType 把类型转换为指定类型。
14 ToUInt16 把类型转换为 16 位无符号整数类型。
15 ToUInt32 把类型转换为 32 位无符号整数类型。
16 ToUInt64 把类型转换为 64 位无符号整数类型。

实例

 			int i = 75;
            float f = 53.005f;
            double d = 2345.7652;
            bool b = true;

            Console.WriteLine(i.ToString());
            Console.WriteLine(f.ToString());
            Console.WriteLine(d.ToString());
            Console.WriteLine(b.ToString());
            Console.ReadKey();

注释

// 单行注释

/**/ 多行注释

/// 文档注释

表达式

类别 表达式 说明
基本 x.m 成员访问
x(...) 方法和委托调用
x[...] 数组和索引器访问
newT(...) 对象和委托创建
newT(...){...} 使用初始值设定项创建对象
new{...} 匿名对象初始值设定项
newT[...] 数组创建
一元 +x 恒等
-x 求相反数
!x 逻辑求反
~x 按位求反
++x 前增量
--x 前减量
x++ 后增量
x-- 后减量
(T)x 将x显示转换为类型T
二元 x * y 乘法
x / y 除法
x % y 取余
x + y 加法,字符串串联
x - y 减法
x << y 位左移
x >> y 位右移
x < y 小于
x > y 大于
x <= y 小于或等于
x >= y 大于或等于
x is T 如果 x 位 T ,返回true,否则false
x as T 返回转换为类型 T 的 x ,如果 x 不是 T 则返回null
x == y 等于
x != y 不等于
x & y 整形按位与 ,布尔逻辑AND
x | y
x && y 且,当 x 为true时,才对 y 求值
`x
x ?? y 如果 x 为null,则计算结果为 y,否则为 x
三元 x ? y : z 如果 x 为true,对 y 求值,x 为false,对 z 求值
赋值或匿名函数 x = y 赋值
x = x + y 复合赋值
(T x) => y 匿名函数(lambda表达式)

判断语句

if 语句 一个 if 语句 由一个布尔表达式后跟一个或多个语句组成。
if...else 语句 一个 if 语句 后可跟一个可选的 else 语句,else 语句在布尔表达式为假时执行。
嵌套 if 语句 您可以在一个 ifelse if 语句内使用另一个 ifelse if 语句。
switch 语句 一个 switch 语句允许测试一个变量等于多个值时的情况。
嵌套 switch 语句 您可以在一个 switch 语句内使用另一个 switch 语句。
for(int i = 0; i<10;i++ ){     }           
while循环
while(true){  }
do-while循环
do{  }while(true)

三元运算符

Exp1 ? Exp2 : Exp3;

其中,Exp1、Exp2 和 Exp3 是表达式。请注意,冒号的使用和位置。

? 表达式的值是由 Exp1 决定的。如果 Exp1 为真,则计算 Exp2 的值,结果即为整个 ? 表达式的值。如果 Exp1 为假,则计算 Exp3 的值,结果即为整个 ? 表达式的值。

循环

循环类型 描述
while 循环 当给定条件为真时,重复语句或语句组。它会在执行循环主体之前测试条件。
for/foreach 循环 多次执行一个语句序列,简化管理循环变量的代码。
do...while 循环 除了它是在循环主体结尾测试条件外,其他与 while 语句类似。
嵌套循环 您可以在 while、for 或 do..while 循环内使用一个或多个循环。

数组

//声明没有元素的数组     
int[] ints = new int[6]     
//声明初始化有元素的数组     
int[] ints = new int[]{1, 3, 4, 5}     
//在声明初始化有元素的数组时,也可以指定数组大小     
string[] strings = new int[5]{"H", “E", "L",“L","0"}

函数

函数的参数设置&传参行为

  • 参数可认为是外部需要函数帮忙处理的数据。
  • 外部通过传递参数的形式,将需要处理的数据交给函数处理。

函数返回值的设置

  • 函数返回值可以认为是外部调用某种行为后得到的一种反馈。

拓展-- 参数修饰符

修饰符种类

  1. 无修饰符:如果一个参数没有用参数修饰符标记,则认为它将按值进行传递,这将意味着被调用的方法收到原始数据的一份副本
  2. out: 输出参数由被调用的方法赋值,因此按引用传递,如果被调用的方法没有给输出参数赋值,就会出现编译错误,也就是说,只要调用了,就必须给赋值。out 最大的用途就是调用者只使用一次方法的调用就能获得多个返回值。(在C#7.0中要实现一次方法的调用就能获得多个返回值,建议使用元组。是元组不是元祖),调用的是指针,是地址
  3. ref: 调用者赋初值,并且可以由被调用的方法可选的重新赋值(数据是按引用传递的)。如果被调用的方法未能给ref参数赋值,也不会有编译器错误。
  4. 了解即可params:这个参数修饰符允许将一组可变的数量的参数作为单独的逻辑参数进行传递, 方法只能有一个params修饰符,而且必须是方法的最后一个参数。
  5. outref的区别
    • out修饰的参数必须在方法内修改,而ref可以修改也可以不修改;
    • out在传入参数的时候,参数是局部变量的话,可以不用赋值,因为out一定会对其进行赋值;
    • ref修饰的参数,在实参必须有初始值才能调用。因为ref修饰的不一定会给它赋值。

结构体

在 C# 中,结构体是值类型数据结构。它使得一个单一变量可以存储各种数据类型的相关数据。struct 关键字用于创建结构体。

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Books books;
            books.title = "C Programming";
            books.author = "Nuha Ali";
            books.subject = "C Programming Tutorial";
            books.book_id = 6495407;

            Console.WriteLine(books.title);
            Console.WriteLine(books.author);
            Console.WriteLine(books.subject);
            Console.WriteLine(books.book_id);
        }
        struct Books
        {
            public string title;
            public string author;
            public string subject;
            public int book_id;
        };
    }
}

结果:

C Programming
Nuha Ali
C Programming Tutorial
6495407

当你定义一个类时,你定义了一个数据类型的蓝图。这实际上并没有定义任何的数据,但它定义了类的名称意味着什么,也就是说,类的对象由什么组成及在这个对象上可执行什么操作。对象是类的实例。构成类的方法和变量称为类的成员。

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Box box;
            Box Box1 = new Box();
        }
        class Box
        {
            public double length;   // 长度
            public double breadth;  // 宽度
            public double height;   // 高度
        }
    }
}

继承

继承是面向对象程序设计中最重要的概念之一。继承允许我们根据一个类来定义另一个类,这使得创建和维护应用程序变得更容易。同时也有利于重用代码和节省开发时间。

当创建一个类时,程序员不需要完全重新编写新的数据成员和成员函数,只需要设计一个新的类,继承了已有的类的成员即可。这个已有的类被称为的基类,这个新的类被称为派生类

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Box box;
            Box Box1 = new Box();
        }
        class Box 
        {
            public double length;   // 长度
            public double breadth;  // 宽度
            public double height;   // 高度
        }
        class boxx :Box
        {

        }
    }
   
}

多态

C# 允许您使用关键字 abstract 创建抽象类,用于提供接口的部分类的实现。当一个派生类继承自该抽象类时,实现即完成。抽象类包含抽象方法,抽象方法可被派生类实现。派生类具有更专业的功能。

请注意,下面是有关抽象类的一些规则:

  • 不能创建一个抽象类的实例。
  • 不能在一个抽象类外部声明一个抽象方法。
  • 通过在类定义前面放置关键字 sealed,可以将类声明为密封类。当一个类被声明为 sealed 时,它不能被继承。抽象类不能被声明为 sealed。
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var shapes = new List<Shape>
        {
            new Rectangle(),
            new Triangle(),
            new Circle()
        };
            foreach (var shape in shapes)
            {
                shape.Draw();
            }
        }
        public class Shape
        {
            public int X { get; private set; }
            public int Y { get; private set; }
            public int Height { get; set; }
            public int Width { get; set; }

            // 虚方法
            public virtual void Draw()
            {
                Console.WriteLine("执行基类的画图任务");
            }
        }

        class Circle : Shape
        {
            public override void Draw()
            {
                Console.WriteLine("画一个圆形");
                base.Draw();
            }
        }
        class Rectangle : Shape
        {
            public override void Draw()
            {
                Console.WriteLine("画一个长方形");
                base.Draw();
            }
        }
        class Triangle : Shape
        {
            public override void Draw()
            {
                Console.WriteLine("画一个三角形");
                base.Draw();
            }
        }

        

    }


    }

接口

namespace ConsoleApp1
{
    interface IMyInterface
    {
        // 接口成员
        void MethodToImplement();
    }

    class InterfaceImplementer : IMyInterface
    {
       

        public void MethodToImplement()
        {
            Console.WriteLine("MethodToImplement() called.");
        }
    }
    class Program
    {
        static void Main()
        {
            InterfaceImplementer iImp = new InterfaceImplementer();
            iImp.MethodToImplement();
        }
    }

    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章