小白初始c# 记录分享

本文介绍了C#编程的基础知识,包括using关键字引入System命名空间,namespace声明,class定义,Main方法作为程序入口点,以及Console.WriteLine和ReadKey的方法使用。此外,还讲解了C#的基本语法,如类、注释、变量类型(如bool、int、double等)以及示例代码的运行截图。
摘要由CSDN通过智能技术生成

一,初识 c#

1,参考代码:

using System;

namespace HelloWorldApplication

{

    /* 类名为 HelloWorld */

    class HelloWorld

    {

        /* main函数 */

        static void Main(string[] args)

        {

            /* 我的第一个 C# 程序 */

            Console.WriteLine("你好 c#");

            Console.ReadKey();

        }

    }

}

运行截图:

 

1.using System:using 关键字用于在程序中包含 System 命名空间,一个程序一般有多个using 语句;

2.namespace声明:一个 namespace 是一系列的类,HelloWorldApplication 命名空间包含了类 HelloWorld;

3.class 声明:类 HelloWorld 包含了程序使用的数据和方法声明,类一般包含多个方法,方法定义了类的行为,在这里HelloWorld 类只有一个 Main 方法;

4.Main 方法:是所有 C# 程序的 入口点,Main 方法说明当执行时 类将做什么动作,Main 方法通过语句 Console.WriteLine("Hello World"); 指定了它的行为。

5.WriteLine 是一个定义在 System 命名空间中的 Console 类的一个方法,该语句会在屏幕上显示消息 “Hello,World!”;

  Console.ReadKey(); 是针对 VS.NET 用户,这使得程序会等待一个按键的动作,防止程序从 Visual Studio .NET 启动时屏幕会快速运行并关闭。

二.c#基础语法:

  1.using 关键字

    在任何 C# 程序中的第一条语句都是:using System

    using 关键字用于在程序中包含命名空间,一个程序可以包含多个 using 语句

  2. class 关键字

    class 关键字用于声明一个类

  3. C# 注释

    多行注释以 /* 开始,并以字符 */ 终止,如下所示:

        /* This program demonstrates

        The basic syntax of C# programming

        Language */

    单行注释是用 '//' 符号表示。例如:

        //end class Rectangle    

  4.成员变量: 变量是类的属性或数据成员,用于存储数据

  成员函数: 函数是一系列执行指定任务的语句,类的成员函数是在类内声明的

  标识符: 标识符是用来识别类、变量、函数或任何其它用户定义的项目

  5.C#关键字

    关键字是 C# 编译器预定义的保留字,这些关键字不能用作标识符

在 C# 中,变量分为以下几种类型:

值类型(Value types) 引用类型(Reference types) 指针类型(Pointer types)

1.值类型(Value types):

    bool        布尔值    

    byte        8 位无符号整数    

    char        16 位 Unicode 字符    

    decimal        128 位精确的十进制值

    double        64 位双精度浮点型    

    float        32 位单精度浮点型    

    int            32 位有符号整数类型    

    long        64 位有符号整数类型    

    sbyte        8 位有符号整数类型    

    short        16 位有符号整数类型    

    uint        32 位无符号整数类型    

    ulong        64 位无符号整数类型    

    ushort        16 位无符号整数类型

参考代码:

using System;

namespace self_introduction

{

    class Program

    {

        static void Main(string[] args)

        {

            string name = "张三";

            int age = 18;

            long studedentnumber=353445631;

            double weight = 45.5d;

            float stature = 1.80f;

            char gender = '男';

            Console.WriteLine("姓名:"+name);

            Console.WriteLine("性别:"+gender);

            Console.WriteLine("年龄:"+age);

            Console.WriteLine("学号:"+studedentnumber);

            Console.WriteLine("体重:"+weight);

            Console.WriteLine("身高:"+stature);

            Console.ReadLine();

        }

    }

}

运行截图:

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值