zoukankan      html  css  js  c++  java
  • 30天C#基础巩固-----序列化,集合

    关于集合的练习。

    ----->计算字符串每个字符出现的次数。

                  Console.WriteLine("请输入?");
                string input = Console.ReadLine();
                Dictionary<char,int> dic=new Dictionary<char, int>();
                for (int i = 0; i <input.Length; i++)       //遍历输入字符,此时它为char类型。
                {
                    if (dic.ContainsKey(input[i]))              //看集合中是否有key,有则使其value++,没有则Add()进去
                    {
                        dic[input[i]]++; 
                    }
                    else
                    {
                        dic.Add(input[i], 1);   
                    }                
                }
                //循环输出
                foreach (KeyValuePair<char, int> item in dic)
                {
                    Console.WriteLine("{0}出现的次数是:{1}",item.Key,item.Value);
                }
                Console.ReadKey();

    image

    ---->序列化

       序列化就是格式化,是指将一个对象以某种格式进行呈现的样子。

    --步骤---->

          ---在需要序列化的类前标记[Serializable]

          ---创建序列化的对象BinaryFormatter

          ---创建流

          ---调用Serialize方法。(Serialize():将对象或具有指定顶级(根)的对象图形序列化为给定流。)

    ---->二进制序列化

               [Serializable]    //需要序列化的标记
                class Address
                {
                  public string Name { get; set; }
                  public int Age { get; set; }
                }
                //BinaryFormatter:以二进制格式将对象或整个连接对象图形序列化和反序列化。
                  BinaryFormatter bf=new BinaryFormatter();        
                using (FileStream fs=new FileStream("data",FileMode.Create,FileAccess.Write))
                {
                    //将对象或具有指定顶级(根)的对象图形序列化为给定流。
                    bf.Serialize(fs, new Address() { Age = 120, Name = "阿辉" });  
                }

    image

    2:反序列化

                //BinaryFormatter:以二进制格式将对象或整个连接对象图形序列化和反序列化。
                  BinaryFormatter bf = new BinaryFormatter();
                using (FileStream fs = new FileStream("data", FileMode.Open, FileAccess.Read))
                {
                    //Deserialize()+将指定的流反序列化为对象图形。
                    Address a = bf.Deserialize(fs) as Address;
                }

    image

    ---->XML序列化

                //XmlSerializer+将对象序列化到 XML 文档中和从 XML 文档中反序列化对象。
                XmlSerializer xf=new XmlSerializer(typeof(Address));    //拱顶
                using (FileStream fs = new FileStream("data.xml", FileMode.Create, FileAccess.Write))
                {
                   //Serialize()+使用指定的 System.IO.Stream 序列化指定的 System.Object 并将 XML 文档写入文件
                    xf.Serialize(fs,new Address(){Name="ahui",Age=12});                
                }

    这里我们的类Address需要设置为public类型的,要不然胡出现下面的错误。

    image

    修改之后就好了。

    image

    2:反XML序列化

                XmlSerializer xf=new XmlSerializer(typeof(Address));
                using (FileStream fs=new FileStream("data.xml",FileMode.Open,FileAccess.Read))
                {
                    Address address= xf.Deserialize(fs) as Address;
                }

    image

    ---->Javascript序列化(JSON格式数据)

                //添加一个引用,Web.Extension,这里不需要流来进行操作。
                JavaScriptSerializer jf=new JavaScriptSerializer();       
                Address a=new Address(){Age=11,Name="ahui"};
                //将对象转换为 JSON 字符串。返回的是string类型的。
                string ser = jf.Serialize(a);       
                Console.WriteLine(ser);
                Console.ReadKey();

    image

  • 相关阅读:
    第十章:Android消息机制
    第九章:四大组件的工作过程
    第八章:理解Window和WindowManager
    第七章:Android动画深入分析
    第六章:Android的Drawable
    第五章:理解RemoteViews
    第四章:View的工作原理
    第三章:View的事件体系
    chr()返回值是当前整数对应的 ASCII 字符。
    遍历从左到右,打印子串在字符串中出现的次数
  • 原文地址:https://www.cnblogs.com/netxiaohui/p/5538499.html
Copyright © 2011-2022 走看看