学习资料:C#入门经典(第七版)
学习计划:一天至少一章,主要是熟悉语法以及背后的原理
编译环境IDE:VSC (Visual Stduio Community)
PS:老忘记的事情
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);
}
}
}
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);
}
}
}
string.replace(char a,char b) 用b代替a
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();
}
}
}
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();
}
}
}
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);
}
}
}
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);
}
}
}
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
{
public int Compare(A t1,A t2)
{
if(t1.val>=t2.val)
{
return 1;
}
else
{
return 0;
}
}
}
static void Main(string[] args)
{
List list = new List();
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);
}
}
}
快捷键
Ctrl+shift+n | 新建项目 |
技巧