2021-1-21(开始复习第二天)

c# 类型转换

隐式/显式转换 

隐式:c#默认的方式进行相应的转换

double d = 3;
int i = (int)d;

显式:强制转换

c#内置的一些数据转换方法

(ToBoolean、ToByte、ToChar、ToDataTime、ToDecimal、ToDouble、ToDouble、ToInt16、ToInt32、ToInt64、ToString、ToSingle......)Convert.ToString(); 基于System.Convert类。

namespace TypeConversionApplication
{
   class StringConversion
  {
     static void main(string[] args)
    {
      int i=75;
      float n=53.005f;
      bool b=true;
      Console.WriteLine(i.toString());
      Console.WriteLine(n.toString());
      Console.ReadKey();

    }
  }
}

C#变量

接受用户输入的值

int num=Convert.ToInt32(Console.ReadLine());

C#常量(程序执行期间不会改变)

整数常量、浮点常量、字符常量

常用的转译/: \\(\)、\'(')......

namesapce EscapeChar
{
  class program
  {
    static void main(string[] args)
   {
      Console.WriteLine("Hello\tWorld\n\n");\
      Console.ReadLine();
   }
  }
}

常量的定义

/*同其他的一些的代码类似*/
const <data_type> <name> = value;

C#运算符 

算数运算符、关系运算符、逻辑运算符、位运算符、赋值运算符、其他运算符

C#判断

if/ if...else.../ 嵌套if语句/ switch语句/ 嵌套switch

三元组判断语句 A?B :C

C#循环 

using System;
namespace Loops
{
   class Program
  {
     static void Main(string[] args)
     {
         int i = 0;
            while (i < 10)
            {
                ++i;
                Console.WriteLine(i);
            }
            /*for/foreach*/
            int[] balance = {1,2,3,4,5,6,7,8,9,10};
            foreach(int j in balance)
            {
                Console.WriteLine(j);
            }
            for (int a = 0; a < 10; a++)
            {
                Console.WriteLine(a+1);
            }
            Console.ReadKey();  
     }
  }
}

C#封装

将一个或者多个项目封闭在一个物理的或者逻辑的包中(可以根据具体的需求设置使用者的访问权限)

public、private、protected、internal、protected internal

  public class  Rectangle
    {
        public double length;
        public double width;
        private double GetArea()
        {
            return length * width;
        }
        public void Display()
        {
            Console.WriteLine("长为{0}", length);
            Console.WriteLine("宽为{0}",width);
            Console.WriteLine("面积为{0}",GetArea());
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Rectangle r = new Rectangle();
            r.length = 5;
            r.width = 6;
            r.Display();
            Console.ReadKey();
        }
    }
 public class  Rectangle
    {
        private double length;
        private double width;
        private double GetArea()
        {
            Console.WriteLine("输入长");
            length = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("输入宽");
            width = Convert.ToDouble(Console.ReadLine());
            return length * width;
        }
        public void Display()
        {
            Console.WriteLine("面积为{0}",GetArea());
            Console.WriteLine("长为{0}", length);
            Console.WriteLine("宽为{0}", width);
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Rectangle r = new Rectangle();
            r.Display();
            Console.ReadKey();
        }
    }

C#方法

定义方法  /  使用方法

修辞符/返回类型/方法名称/参数列表/主体

 public int FindMax(int num1,int num2)
 {
     int result=num1 > num2 ? num1:num2;
     return result;
 }

方法可以进行自我调用俗称递归

public int factorial(int num)
{
    int result;
    result = num;
    if (num == 1)
    {
      Console.WriteLine(num);
      return num;
    }
    else
    {          
      result = factorial(num - 1);
      Console.WriteLine(num);
      return result;
    }
}

参数的传递

按引用传递参数 / 按照输出传递参数

按照引用传递(ref)

 public void swap(ref int x,ref int y)
{    
   Console.WriteLine("x为{0},y为{1}", x, y);
   int temp;
   temp = x;
   x = y;
   y = temp;
   Console.WriteLine("x为{0},y为{1}", x, y);
}

按输出传递参数(out)

public void getvaule(out int x)
{
   int temp =11;
   x = temp;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值