zoukankan      html  css  js  c++  java
  • 复习.Net基础

    理解全局变量和局部变量的区别:①作用域②存储方式③生命周期④使用方式


    第一、全局变量的作用域为整个程序,局部变量为当前函数或循环等;

    第二、全局变量存储在全局数据区中,局部变量存储在栈区;

    第三、全局变量和主程序的生命周期一样,随程序的销毁而销毁,局部变量在函数或循环内部,随函数或循环的退出而销毁;

    第四、全局变量在声明之后程序的各个部分都可以使用,但局部变量只能在局部使用,函数内部会优先使用局部变量再使用全局变量。

    常见问题:对于int,double,float等数组,没有赋值的元素初始化为0;对于string等类型初始化为null。

    递归:①阶乘②斐波那契数列

    递归实现:

     1 //阶乘:5!=5*4*3*2*1
     2 
     3 static int JieCheng(int n)
     4 {
     5     if(n<0)
     6     {
     7         Console.WriteLine("输入不能为负数,必须为正整数");
     8     }
     9     else if(n == 1)
    10     {
    11         return 1;
    12     }
    13     else
    14     {
    15         return n*JieCheng(n-1);
    16     }
    17 }
    阶乘
     1 // 斐波那契数列
     2 
     3 static int Fb(int n)
     4 {
     5     if (n<0)
     6     {
     7         Console.WriteLine("输入不能为负数!");
     8     }
     9     if(n==0 || n==1)
    10     {
    11         return 1;
    12     }
    13     else
    14     {
    15         return Fb(n-1)+Fb(n-2);
    16     }
    斐波那契数列

    非递归实现:

     1 // 阶乘
     2 
     3 static int JieCheng(int n)
     4 {
     5     if(n<0)
     6     {
     7         Console.WriteLine("输入不能为负数");
     8     }
     9     else
    10     {
    11         int result = 1;
    12         for(int i=1;i<=n;i++)
    13         {
    14             result = result*i;
    15         }
    16         return result;
    17     }
    18 }
    阶乘
     1 static int Fb(int n)
     2 {
     3     if(n<0)
     4     {
     5         Console.WriteLine("输入不能为负数");
     6     }
     7     if(n==1 || n==0)
     8     {
     9         return 1;
    10     }
    11     else
    12     {
    13         int[] nums = new int[n];
    14         nums[0]=1;
    15         nums[1]=1;
    16         for(int i=2;i<n;i++)
    17         {
    18             nums[i]=nums[i-1]+nums[i-2];
    19         }
    20         return nums[n-1];
    21     }
    22 }
    斐波那契数列

    “洗牌算法”:

     1 int[] nums = new int[100];
     2 for(int i=0;i<nums.Length;i++)
     3 {
     4     nums[i]=i+1;
     5 }
     6 Random rand = new Random();
     7 for(int i=0;i<nums.Length;i++)
     8 {
     9     int index1 = rand.Next(0,nums.Length);
    10     int index2 = rand.Next(0,nums.Length);
    11 
    12     int temp = nums[index1];
    13     nums[index1] = nums[index2];
    14     nums[index2] = temp;
    15 }
    洗牌算法

    关于Null 与 值类型:值类型(int float long boll 结构体 枚举)不能为null,因为其赋值过程是拷贝传递。

    最简单、最实用的“饿汉式”单例模式:

     1 class SingleInstance
     2 {
     3   private static readonly SingleInstance singleinstance = new SingleInstance();
     4   private SingleInstance()
     5    {}
     6   public static SingleInstance GetInstance()
     7   {
     8     return singleinstance;
     9   }
    10 }
    单例模式

    关于static静态构造函数:

    第一、静态构造函数无访问修饰符;

    第二、静态构造函数不能被继承;

    第三、一个类中只能有一个静态构造函数,且只运行一次。

    对“多态”的理解:父类变量可以指向子类对象,父类变量可以执行那些方法取决于父类有那些方法,具体执行则由子类去实现。

    自定义异常类:

    1 public class MyException:Exception
    2 {
    3   public MyException(string msg):base(msg)
    4    {
    5     }
    6 }
    MyException

    流(Stream):所谓的“流”就是读一点操作一点。

    写文件:

    1 using(Stream s = new FileStream(@"F:1.txt",FileMode.Append))
    2 {
    3   byte[] bytes = Encoding.UTF8.GetBytes("Hello World!");
    4   s.Write(bytes,0,bytes.Length);
    5 }
    写文件

    读文件:

     1 using(FileStream fs = new FileStream(@"F:1.txt",FileMode.Open))
     2 {
     3   byte bytes = new byte[4];
     4   int len;
     5   while(len = fs.Read(bytes,0,bytes.Length)>0)
     6   {
     7       string s = Encoding.UTF8.GetString(bytes,0,len);
     8       Console.WriteLine(s);
     9    }
    10 }
    读文件

    考虑到中文字符的编码问题,应使用StreamReader、StreamWriter。

    写数据:

    1 static void MyStreamWriter(string path,string content)
    2 {
    3   using(Stream inStream = new FileStream(path,FileMode.Create))
    4   using(StreamWriter writer = new StreamWriter(inStream))
    5   {
    6       writer.WriteLine(content);
    7    }
    8 }
    写数据

    读数据:

     1 static string MyStreamReader(string path)
     2 {
     3   StringBuilder sb = StringBuilder();
     4    using(Stream outStream = new FileStream(path,FileMode.Open))
     5    using(StreamReader reader = new StreamReader(outStream,Encoding.Default)) 
     6    {
     7       while(reader.Peek()>-1)
     8       {
     9          sb.AppendLine(reader.ReadLine());//不能使用while(reader.ReadLine()!=null)不然会出现异常
    10       }
    11    }
    12     return sb.ToString();
    13 }    
    读数据

    泛型容器

     1 List<int> list = new List<int>();
     2 list.Add(1);
     3 list.Add(2);
     4 list.AddRange(new int[]{1,2,3});
     5 list.AddRange(list);
     6 
     7 //List泛型集合可以转换为数组
     8 int[] nums = list.ToArray();
     9 List<string> listStr = new List<string>;
    10 string[] strs = listStr.ToArray();
    List

    泛型字典

    1 Dictionary<string,string> dict = new Dictionary<string,string>;
    2 dict.Add("zs","张三");
    3 dict.Add("ls","李四");
    4 dict.Add("ww",王五");
    5 dict["ls"] = "小赵";
    6 string s = dict["ww"];
    7 dict.ContainKey("zs");
    Dictionary

    关于foreach,只有实现了IEnumrable接口都可使用foreach遍历。

     

  • 相关阅读:
    Redis学习笔记(十四)Sentinel(哨兵)(上)
    Redis学习笔记(十三) 复制(下)
    Redis学习笔记(十二) 复制(上)
    搞懂推荐系统中的评价指标NDCG(CG、DCG、IDCG)
    pytorch入门3.2构建分类模型再体验(批处理)
    pytorch入门3.1构建分类模型再体验(模型和训练)
    pytorch入门3.0构建分类模型再体验(准备数据)
    一条一条的读《推荐系统实战》2.0
    pytorch入门2.2构建回归模型初体验(开始训练)
    pytorch入门2.1构建回归模型初体验(模型构建)
  • 原文地址:https://www.cnblogs.com/HuShaoyi/p/8433244.html
Copyright © 2011-2022 走看看