C#入门经典(第七版) 学习笔记

C#经典入门书籍推荐(5本) 本书详细介绍了C#语言本身的语法规范,内容包括.NET框架基础类库的特点及其用法,以及控制台应用、类库、Windows窗体应用、Windows服务、Windows控件、Web窗体应用、Web服务器控件、Web服务、基于远程处理的分布式应用等具体知识体系。同时本书还详细讨论了消息组件、操作进程、网络编程、XML编程等C#中编程的热点问题。随书超值赠送的光盘包括本书实例的源代码。本书实例丰富、内容新颖、实用性强,适用于C#爱好者和C#程序设计人员,并可供对.NET感兴趣的读者参考。 阅读详情

学习资料:C#入门经典(第七版)

学习计划:一天至少一章,主要是熟悉语法以及背后的原理

编译环境IDE:VSC (Visual Stduio Community)

PS:老忘记的事情

  • 类里面要写清楚数据是可访问类型是哪种
  • foreach一个list,取出来的是object类型,要强制转换成对应派生类

第二章 编写C#程序

  1. Console.ReadKey():等待一个响应,即任意敲击键盘一次
  2. 右侧解决方案中,program.cs文件双击某个函数,可以跳转到对应的位置,起到了目录跳转的作用

第三章 变量和表达式

  1. 输出某种类型的最大值, type.MaxValue
  2. 输出多个变量Console.WriteLine($"{a},{b}");  Console.WriteLine("{0},{1}",a,b);这两个区别在于$和括号内容。0base
  3. 变量命名必须以 _ 字母 @开头
  4. @+string,可以让string里不再用转移字符
  5. 一元运算符'+'并不能把负数变正。是个比较奇怪的运算符,作用不明
  6. console是个静态类,不能被初始化

第四章 流程控制

  1. switch每一个case必须得break,这是和C++不同的地方

第五章 变量的更多内容

  1. 枚举的用法
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        enum temp : int
        {
            a=1,
            b=2
        }
        class Program
        {
            static void Main(string[] args)
            {
                temp tt = temp.a;
                int t = (int)temp.a;
                Console.WriteLine("{0}   {1}",tt,t);
            }
            
        }
    }
    
  2. string.Trim(),可以从头开始去掉一些想删除的字符,从后开始删除一些想要删除的字符,直到没有或者当前字符不是被删字符数组的内容。string.TrimStart(), string.TrimEnd()字面意思理解一下。
  3. string.PadLeft(len,char),左边补空格到指定长度。当然还有PadRight(len)
  4. string.split(char[] or string []) both are ok,按照char[]中的字符或者string[]中的字符串来分割给定字符串
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string s = Console.ReadLine();
                string[] word;
                string[] c = new string[2] { "aa", "dd" };
                word = s.Split(c,System.StringSplitOptions.RemoveEmptyEntries);
                foreach (string t in word)
                    Console.WriteLine(t);
            }
            
        }
    }
    

  5. string.replace(char a,char  b) 用b代替a

  6. var相当于C++中的auto

第六章 函数

  1. 函数用static
  2. 当使用params的时候,由于这是一个静态方法。当要定可变参数个数的时候,static  int cul(params int[] a)
  3. 对于静态方法还得研究一下,这个问题和第二个问题有关。
    1. solve it !  我们用的类class program其实是未定义的,但static是已经好了的可以直接用。要么我们新new program要么就定static
  4. C#中的引用,包括ref和out两种,形式基本相同。但,ref要求有初值,out在到目标函数的时候会丢失数据
  5. console.writeline和todouble可以通过引入头文件来解决。
  6. type.tryparse(string,out name) 检查string是否type类型,若是则赋值给name,返回真;否则返回假
  7. 委托不是很懂,等后面看到事件应该会明白些。暂时写了个readline的委托,writeline没写出来

第八章 面向对象编程

  1. OOP面向对象编程,UML统一建模语言
  2. C#一切皆对象,其中用  实例对象.方法/属性,方法和属性用()区别
  3. 静态构造函数用于初始化类中的静态成员,在创建实例化对象和访问该静态成员时会被默认调用
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using static System.Console;
    using  System.Diagnostics;
    using static System.Convert;
    namespace ConsoleApp1
    {
        class Program
        { 
            public class A
            {
                public int a;
                static int b;
                static A()
                {
                    WriteLine("static");
                    b = 2;
                }
                public A()
                {
                    WriteLine("default");
                    a = 1;
                }
            }
            static void Main(string[] args)
            {
                A temp=new A();
            }
            
        }
    }
    

  4. 静态类只包含静态成员(属性和方法),并且每个成员都要指定共有还是私有,除了被static修饰的
  5. 接口最好不要随便改动,因为这是一个公共构建。如果需要改动,可以将接口扩展(创建一个新接口)。每个类继承一个接口后,可以避免写很多具有相同方法的类对象重载,有利于维护。接口有多态性,可以针对不同的类的相同方法。(BLOG)——面向接口编程
    using System;
    using static System.Object;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using static System.Console;
    using  System.Diagnostics;
    using static System.Convert;
    namespace ConsoleApp1
    {
        class Program
        { 
            public interface IMy_interface
            {
                void show();
            }
            public class A : IMy_interface
            {
                public void show()
                {
                    WriteLine("haha");
                }
            }
            static void Main(string[] args)
            {
                IMy_interface temp = new A();
                temp.show();
            }
            
        }
    }
    
  6. System.object 是有所类的基类,万物皆对象。。!

第九章 定义类

  • 构造函数可以给父类传递参数,也可以指定当前类要转移到当前的哪个构造函数
  • 类的赋值是引用
  • 有浅拷贝和深拷贝,和C++一样 (未完成)

第十章 定义类成员

  • 公有变量用PscalCasing命名,帕斯卡拼写法。私有变量用camelCsing,骆驼拼写法。是两种命名规范
  • readonly代表该变量只能在构造函数中赋值
  • 静态成员只能通过 类名.成员  来调用
  • override,代表方法重写
  • set和get访问器key可以对赋值的内容进行封装(设立条件才能赋值,否则返回)。这样使用者就看不到内部的代码
    • 两种写法,一种定义一个新的属性。或者直接public int x{set;get;}编译器会自动生成一个新的属性
  • 当函数重写(虚函数)想调用基类的某个函数,那么用base.函数名()。this.就用当前实例的某个成员,能看得更加清晰

第十一章 集合、比较和转换

  • 集合
    • 建立动态长度集合
    • 自定义集合(存在问题)
      using System;
      using static System.Object;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Collections;
      namespace ConsoleApp1
      {
          class Program
          { 
              public class Animal
              {
                  public string Name;
                  public Animal (string s)
                  {
                      Name = s;
                  }
              }
              public class Animals : CollectionBase
              {
                  public void Add(Animal a)
                  {
                      List.Add(a);
                  }
                  public void Remove(Animal a)
                  {
                      List.Remove(a);
                  }
                  public Animals() { }
              }
              static void Main(string[] args)
              {
                  Animals temp = new Animals();
                  temp.Add(new Animal("123"));
                  temp.Add(new Animal("456"));
                  foreach (Animal ani in temp) Console.WriteLine(ani.Name); //不知道为什么foreach中Animal写var会报错
              }
              
          }
      }
      
    • 定义索引符,有点像重载函数。

      using System;
      using static System.Object;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Collections;
      namespace ConsoleApp1
      {
          class Program
          { 
              public class Animal
              {
                  public string Name;
                  public Animal (string s)
                  {
                      Name = s;
                  }
              }
              public class Animals : CollectionBase
              {
                  public void Add(Animal a)
                  {
                      List.Add(a);
                  }
                  public void Remove(Animal a)
                  {
                      List.Remove(a);
                  }
                  public Animal this[int index]
                  {
                      get { return (Animal)List[index]; }
                      set { List[index] = value; }
                  }
              }
              static void Main(string[] args)
              {
                  Animals temp = new Animals();
                  temp.Add(new Animal("123"));
                  temp.Add(new Animal("456"));
                  Console.WriteLine(temp[1].Name);
              }
              
          }
      }
      

       

    • 使用字典

    • 自定义字典

      using System;
      using static System.Object;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Collections;
      namespace ConsoleApp1
      {
          class Program
          { 
              public class Animal
              {
                  public string Name;
                  public Animal (string s)
                  {
                      Name = s;
                  }
              }
              public class Animals : DictionaryBase
              {
                  public void Add(Animal a,int v)
                  {
                      Dictionary.Add(a,v);
                  }
                  public void Remove(Animal a)
                  {
                      Dictionary.Remove(a);
                  }
                  public Animal this[Animal a]
                  {
                      get { return (Animal)Dictionary[a]; }
                      set { Dictionary[a] = value; }
                  }
              }
              static void Main(string[] args)
              {
                  Animals temp = new Animals();
                  temp.Add(new Animal("123"),456);
                  temp.Add(new Animal("222"), 333);
                  foreach (DictionaryEntry t in temp)
                      Console.WriteLine((t.Value));
              }
              
          }
      }
      

       

    • 实现一个迭代器,每次用yield return value 来返回值,使用接口IEnumerable。foreach每次的变量是value

    • 迭代器遍历字典(未完成)

      using System;
      using static System.Object;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Collections;
      namespace ConsoleApp1
      {
          class Program
          { 
              public class Animal
              {
                  public string Name;
                  public Animal (string s)
                  {
                      Name = s;
                  }
              }
              public class Animals : DictionaryBase
              {
                  public void Add(Animal a, Animal v)
                  {
                      Dictionary.Add(a,v);
                  }
                  public void Remove(Animal a)
                  {
                      Dictionary.Remove(a);
                  }
                  public Animal this[Animal a]
                  {
                      get { return (Animal)Dictionary[a]; }
                      set { Dictionary[a] = value; }
                  }
                  public IEnumerable GetEnumerable()
                  {
                      foreach(object MyAnimal in Dictionary.Values)
                      {
                          yield return (Animal)MyAnimal;
                      }
                  }
              }
              static void Main(string[] args)
              {
                  Animals temp = new Animals();
                  temp.Add(new Animal("123"),new Animal("456"));
                  temp.Add(new Animal("222"), new Animal("333"));
                  foreach (Animal MyAnimal in temp)
                      Console.WriteLine(MyAnimal.Name);
              }
              
          }
      }
      

       

    • 深复制(继承接口ICloneable)和浅复制(MemberwiseClone())

  • 比较

    • 拆箱和装:装箱和拆箱实质上是引用类型和值类型的转换

    • is运算符。A是B的派生类,A继承接口B,A可以拆箱到B

    • 重载+运算符
using System;
using static System.Object;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApp1
{
    class Program
    { 
        class A
        {
            public int val;
            public A(int v)
            {
                val = v;
            }
            public static A operator+(A temp1,A temp2)
            {
                A NewTemp = new A(0);
                NewTemp.val= temp1.val + temp2.val; 
                return NewTemp;
            }
        }
        static void Main(string[] args)
        {
            A t1 = new A(1);
            A t2 = new A(2);
            t1 = t1 + t2;
            Console.WriteLine(t1.val);
        }
        
    }
}
  • 类内实现IComparable,类外实现IComparer,后者有点像自定义CMP,前者像类内重载。
    • 利用IComparable实现集合排序
      using System;
      using static System.Object;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Collections;
      namespace ConsoleApp1
      {
          class Program
          { 
              public class A : IComparable
              {
                  public int val;
                  public A(int v)
                  {
                      val = v;
                  }
                  public int CompareTo(object obj)
                  {
                      if(this.val >= ((A)obj).val) { return 1; }
                      else
                      {
                          return 0;
                      }
                  }
              }
              static void Main(string[] args)
              {
                  ArrayList list = new ArrayList();
                  list.Add(new A(3));
                  list.Add(new A(4));
                  list.Add(new A(3));
                  list.Sort();
                  for (int i = 0; i < list.Count; i++)
                      Console.WriteLine((list[i] as A).val);
              }
              
          }
      }
      

       

    • 利用IComparer实现集合排序
      • using System;
        using static System.Object;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        using System.Collections;
        namespace ConsoleApp1
        {
            class Program
            { 
                public class A 
                {
                    public int val;
                    public A(int v)
                    {
                        val = v;
                    }
                }
        
                public class CMP : IComparer<A>
                {
                    public int Compare(A t1,A t2)
                    {
                        if(t1.val>=t2.val)
                        {
                            return 1;
                        }
                        else
                        {
                            return 0;
                        }
                    }
                }
        
                static void Main(string[] args)
                {
        
                    List<A> list = new List<A>();
                    list.Add(new A(3));
                    list.Add(new A(4));
                    list.Add(new A(3));
                    list.Sort(new CMP());//arraylist不允许这么用
                    for (int i = 0; i < list.Count; i++)
                        Console.WriteLine(((A)list[i]).val);
                }
                
            }
        }
        

         

    • 转换 a as T,将a变量转化为T类型的变量,如果不能隐式转换则为null

第十二章 泛型

  • 可空类型:int? a;
  • a??b 等价a==null?b:a,和三目表达式一样

 

快捷键

Ctrl+shift+n新建项目
  
  

 

 

 

 

技巧

  1. 自己可以根据习惯,设定一些快捷键,设置注释和解除注释快捷键。个人是用惯了codeblock的快捷键,把注释设置为Ctrl+Shift+C,解除设置为Ctrl+Shift+X
C#:扑克牌游戏(1):规划CardLib类库开发扑克牌游戏 (一):前言         这是《C#入门经典第七版)》的学习笔记,接下来会通过一个扑克牌程序学习关于C#相关知识。 (二):前期思考过程         创建一个类库,命名应该为CardLib,不过按照书上的安排,该类库是在第十章第一次使用,在此命名为Ch10CardLib。         容易想到,我们平时玩扑克牌的时候,总是会说,一副两副,因此我们考虑创建一个类Deck代表“一... 阅读详情

相关推荐

C#入门经典第七版读书笔记1(C#简介)

C#简介

moonfish0607的博客 908

c#入门经典 第十版 附录练习题答案

C#9 and .NET 5 练习题及答案

C#图解教程经典C#入门

C#图解教程》是一本经典C#入门书,不仅适合没有任何编程语言基础的初级读者,而且还是有VB、C++等语言基础的C#初学者的最佳选择。 用图说话,最易学的C#教程,Amazon全五星盛誉,涵盖VisualC#2008和.NET3.5最新特性。 作为.NET平台上最主要的编程语言,C#在不断地改进和完善,功能越来越强大。当然,复杂性也随之增加。令很多初学者不得其门而入。 如何才能

u013736471的专栏 2451

C#入门经典中文版(第三版)pdf版本8

C#入门经典中文版(第三版)pdf版本8

C#编程基础(万字详解,这一篇就够了)

C# 是一个现代的、通用的、面向对象的编程语言,它是由微软(Microsoft)开发的,由 Ecma 和 ISO 核准认可的。C# 是由 Anders Hejlsberg 和他的团队在 .Net 框架开发期间开发的。C# 是专为公共语言基础结构(CLI)设计的。CLI 由可执行代码和运行时环境组成,允许在不同的计算机平台和体系结构上使用各种高级语言。现代的、通用的编程语言。面向对象。面向组件。容易学习。结构化语言。它产生高效率的程序它可以在多种计算机平台上编译。

m0_58367586的博客 10万+

C# 入门经典

1 C#简介 .NET Framework是Microsoft最新的开发平台,目前版本是4。它包括一个公共类型系统(CTS)和一个公共语言运行时(CLR)。 用.NET Framework编写的应用程序首先编译为CIL(以前叫MSIL)。在执行应用程序时,JIT把CIL编译为本机代码。应用程序编译后,把不同的部分链接到包含CIL的程序集中。 3 变量和表达式 以#开头的任意关键字实

timewalker08的专栏 1771

每天进步一点点,《C#入门经典第七版学习笔记

今天才知道,原来EXE是可以在命令行直接运行的。酷! 截图

weixin_42528820的博客 254

C#入门经典(第7版).pdf

抱歉抱歉,当时没有看里面的内容。 C#高级编程(第七版) 链接:https://pan.baidu.com/s/1RWM6uM9CjQHUHLOMgitKeg 密码:q1ou C#入门经典(第六版、第七版) 链接:https://pan.baidu.com/s/1yFh5si2fBP8WGItdCapvCQ 提取码:9fu8 ...

sxy_qjj的博客 5万+

二蛋赠书三期:《C#入门经典(第9版)》

大家好!我是二蛋,一个热爱技术、乐于分享的工程师。在过去的几年里,我一直通过各种渠道与大家分享技术知识和经验。我深知,每一位技术人员都对自己的技能提升和职业发展有着热切的期待。因此,我非常感激大家一直以来对我的关注和支持。为了回馈大家的厚爱,我决定启动一项特别的赠书活动。我希望通过这个活动,能够让更多的读者获得有价值的技术支持,并提高自己的技能水平。在这个活动中,我将不定期向大家赠送一本技术相关书籍。这些书籍涵盖了各种技术领域,包括编程、人工智能、大数据等等。

二蛋的博客 5343

C#2010入门经典beginning c# 2010(英文版+源代码)

一本久负盛名的红皮书,一本描述C#2010的书,外加从官网下载的全书源代码。

c#入门到精通pdf

c#入门到精通 c#入门到精通 c#入门到精通 c#入门到精通

C#入门经典 第6版[扫描版PDF电子书] .pdf

C#入门经典 第6版[扫描版PDF电子书] .pdf C#入门经典 第6版[扫描版PDF电子书] .pdf

C#入门经典(第7版)

C#入门经典(第7版) C# 6.0 & Visual Studio 2015_2016.08_P701——试读PDF电子书下载 带索引书签目录高清版

C#入门经典(第七版)

结构清晰,叙述清楚。无论是刚开始接触面向对象编程的新手,还是打算迁移到C#的C、C++或Java程序员,都可以从《C#入门经典(第七版)》汲取到新的知识。迅速掌握C#编程技术。

C入门经典第七版资源下载介绍:一本不可或缺的编程宝典

C#入门经典第七版资源下载介绍:一本不可或缺的编程宝典 去发现同类优质开源项目:https://gitcode.com/ C#入门经典第七版资源下载,助你轻松掌握C#编程核心技术,深入理解面向对象编程。 项目介绍 《C#入门经典第七版)》是一本深受编程爱好者欢迎的经典教材。该书内容丰富,结构清晰,语言简洁易懂,旨在帮助读者从零开始学习C#编程语言,同时也适用于希望从其他编程语言转型的程序员。通过...

gitblog_06798的博客 535

C入门经典(第七版)资源下载介绍

C#入门经典(第七版)资源下载介绍 去发现同类优质开源项目:https://gitcode.com/ 《C#入门经典(第七版)》是一本深受欢迎的编程书籍,内容结构清晰,叙述简洁易懂。本书面向不同层次的读者,无论是刚开始接触面向对象编程的新手,还是希望从C、C++或Java等其他语言迁移到C#的程序员,都能从中获取宝贵的知识和技能。 本书将帮助你迅速掌握C#编程技术,深入理解C#语言的各个方面,包括...

gitblog_06760的博客 402

<C#入门经典>学习笔记1之初识C#

序言 选择《 C#入门经典第五版》作为自学书籍,以此记录学习过程中的笔记与心得。C#简介 1. C#是一种块结构的语言 2. C#区分大小写C#变量 C#的变量定义与C语言类似一、变量类型及定义 整形及范围定义 浮点型及范围定义 float和double以±m∗2 e  ±m*2^e的形式存储浮点数 Decimal以±m

baidu_34513951的博客 4999

C#经典书籍推荐

C#经典书籍推荐 .NET大局观(第2版) 电子工业出版社 / 39元 Programming C#中文版:第4版 [美]里伯提(Liberty,J.)著;刘基诚,李愈胜,刘卫卫译 /电子工业出版社 / 68元 Visual C# 2005...

alieen_941124的博客 1249
上一篇: 操作系统 期末复习
下一篇: 软件工程(第三版) 期末复习
Conchpeng
博客等级 码龄9年 216粉丝 514原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值