C#入门(详细)

Hello C#

using System;

namespace Program
{
    class helloworld
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello C#");
            Console.ReadLine();
        }
    }

}

C#数据类型

1.值类型(直接把数据存在内存栈上面):

C#中很多内键类型都是值类型

如:

int(System.Int32的简写)

float(System.Single)

bool(System.Boolean的简写)

值类型的数据都是继承System.ValueType,

System.ValueType又是继承System.Object,

Object是C#的一个基类,在C#中所有的类型都是继承这个类的。

2.引用类型(在内存栈上面只存了一个引用,数据是存在内存杯堆面的)

3.指针类型

结构体

主要功能:在C#中结构体也是值类型的一个数据,可以让一个单一的变量存储多种数据

 public static void Main(string[] args)
        {
            Student s1 = new Student();
            s1.name = "张三";
            s1.age = 1;
            Console.WriteLine("name:" + s1.name + " age: "+s1.age);
            Console.ReadLine();
        }
        struct Student
        {
            public int age;
            public string name;
        }

在结构体中也可以声明一个方法

this就是当前这个对象

 public static void Main(string[] args)
        {
            Student s1 = new Student();
            Student s2 = new Student();
            s1.name = "张三";
            s1.age = 1;
            s2.name = "a";
            s2.age = 2;
            s1.Print();
            s2.Print();
            Console.ReadLine();
        }
        struct Student
        {
            public int age;
            public string name;
            public void Print()
            {
                Console.WriteLine("name:" + this.name + " age: " + this.age);
            }
        }

枚举类型

主要功能:限定一个值的取值范围

使用方法:

如:

 public static void Main(string[] args)
        {
            Student s1 = new Student();
            Student s2 = new Student();
            s1.name = "张三";
            s1.age = 1;
            s1.sex = Sex.Man;

            s2.name = "a";
            s2.age = 2;
            s2.sex = Sex.Woman;
            s1.Print();
            s2.Print();
            Console.ReadLine();
        }
        struct Student
        {
            public int age;
            public string name;
            public Sex sex;
            public void Print()
            {
                Console.WriteLine("name:" + this.name + " age: " + this.age + " sex:" + this.sex);

            }
        }
        enum Sex
        {
            Man,
            Woman
        }

常量

声明时在数据类型前面加上关键字const

运算符(有点类似C语言)

算术运算符

+ - */ % ++ --

关系运算符

== != > < >= <=

逻辑运算符

// && || !

位运算符

& ^ | << >>

赋值运算符

= += -= /= %= <<= >>=

其他运算符

sizeof

typeof

? :(三目运算符)

is

as(适用于可以为空的类型转换)

判断语句

if语句

if……else语句

嵌套if

switch语句

如:

  int day = 1;
            string str = "abc";
      
            switch(day)
            {
                case 1:
                    str = "今天是星期一";
                    break;
                case 2:
                    str = "今天是星期二";
                    break;
                case 3:
                    str = "今天是星期三";
                    break;
                case 4:
                    str = "今天是星期四";
                    break;
                case 5:
                    str = "今天是星期五";
                    break;
                case 6:
                    str = "今天是星期六";
                    break;
                case 7:
                    str = "今天是星期日";
                    break;
            }
            Console.WriteLine(str);
            Console.ReadLine();

嵌套switch语句(不建议这样使用,因为这样不太直观,建议使用其他判断语句嵌套)

如:

            int a = 1;
            int b = 2;
            switch(a)
            { 
                case 1:
                    switch (b)
                    {
                        case 2:
                            Console.WriteLine("nihao");
                            Console.ReadLine();
                            break;
                    }
                    break;
            }

C#循环

while循环

for/foreach循环

do……while循环

            Console.WriteLine("nihao");
                        Console.ReadLine();
                        break;
                }
                break;
        }

## C#循环

while循环

for/foreach循环

do……while循环

嵌套循环
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值