C#基础1——语法

C#基础1——语法

(一) .net  ,visual studio , c# 之间的关系  

 

 

(二)VS开发环境的介绍

 

 (三)c#语法

 

(二)c#程序

        控制台的三条指令:

        Console.WriteLine:打印输出

        Console.ReadLine:从控制台读入一行

        Console.ReadKey:控制台暂停。

例1:

 

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("hello");
            Console.ReadKey();          //按一个按键继续执行
        }
    }
}

例2: Console.ReadLine应用

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("hello");
            string s = Console.ReadLine();//用户输入文字的时候程序是暂停的,用户输入完毕点回车,把用户输入的东西作为返回值,声明一个string类型的变量(容器)s,用s来放WriteLine函数返回的值。 
            Console.WriteLine(s);
            Console.ReadKey();          //按一个按键继续执行
        }
    }
}

例3:“+”,连接两个字符串

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("hello");
            string s = Console.ReadLine();
            Console.WriteLine(s+"你好"); 

            int i1 = 10;
            int i2 = 20;
            Console.WriteLine(i1+"+"+i2+"="+(i1+i2));

            Console.ReadKey();          
        }
    }
}


例4:占位符的形式表示“i1+i2=i3”

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i1 = 10;
            int i2 = 20;  
            //占位符从0开始,顺序就是WriteLine第二个参数开始的顺序
            Console.WriteLine("{0}+{1}={2}",i1,i2,i1+i2);     //占位符的形式表示
            Console.ReadKey();          
        }
    }
}


例4.2:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入男孩的名字");
            string boy = Console.ReadLine();
            Console.WriteLine("请输入女孩的名字");
            string girl = Console.ReadLine();
            Console.WriteLine("{0}爱{1}",boy,girl);
            Console.ReadKey();          
        }
    }
}

例5:c#从上到下一条条依次执行

       注意下面两段代码执行后第二次输出c的区别


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 3;
            int j = 5;
            int c = a + j;
            Console.WriteLine(c);
             a = 2;
            Console.WriteLine(c);
            Console.ReadKey();
        }
    }
}


 

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 3;
            int j = 5;
            int c = a + j;
            Console.WriteLine(c);
             a = 2;
            Console.WriteLine(c);
            Console.ReadKey();
        }
    }
}

 


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 3;
            int j = 5;
            int c = a + j;
            Console.WriteLine(c);
             a = 2;
             c = a + j;
            Console.WriteLine(c);
            Console.ReadKey();
        }
    }
}


 

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 3;
            int j = 5;
            int c = a + j;
            Console.WriteLine(c);
             a = 2;
             c = a + j;
            Console.WriteLine(c);
            Console.ReadKey();
        }
    }
}


例6:c#的注释

单行注释的表示:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            
            int a = 3;
            int j = 5;
            int c = a + j;
            Console.WriteLine(c);
            a = 2;
            Console.WriteLine(c);    //此处很好的体现了“c#从上到下一条条依次执行
            Console.ReadKey();
        }
    }
}


 

多行注释的表示

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            /*
            Console.WriteLine("请输入男孩的名字");
            string boy = Console.ReadLine();
            Console.WriteLine("请输入女孩的名字");
            string girl = Console.ReadLine();
            Console.WriteLine("{0}爱{1}", boy, girl);
            */

            int a = 3;
            int j = 5;
            int c = a + j;
            Console.WriteLine(c);
            Console.ReadKey();
        }
    }
}

 


 

 


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("hello");
            string s = Console.ReadLine();//用户输入文字的时候程序是暂停的,用户输入完毕点回车,把用户输入的东西作为返回值,声明一个string类型的变量(容器)s,用s来放WriteLine函数返回的值。 
            Console.WriteLine(s);
            Console.ReadKey();          //按一个按键继续执行
        }
    }
}


 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值