My personal summary goes deep into the. NET platform and C # programming,

Source: Internet
Author: User

My personal summary goes deep into the. NET platform and C # programming,

Preface:I have been studying C # programming for several months. As a cainiao, I am not qualified to comment on anything. We can only summarize what we have learned, but may not be rigorous.

1. In-depth. NET Framework

. NET Framework is the core foundation for developing. NET applications.

. NET Framework Architecture

Supports the development of C #, VB,. NET, c ++, and other languages, that is, cross-language development.

The. NET Framework has two main components: CLR and FCL. (CLR is the Common Language Runtime, that is, the public Language; FCL is the Framework Class Library, that is, the Framework Class Library)

. NET Framework core class library and its Functions

 

Class and Object

Class defines a set of conceptual models, and objects are real entities.

Set accessors write only; get accessors read only.

In vs, automatically attribute prop + double-click the Tab key

Encapsulation

1. ensure data security.

2. Provide clear external interfaces

3. The class can be modified without affecting other classes.

Class Diagram

 

Ii. go deep into C # Data Types

Value Type Application Type

Value types include basic data types, enumeration types, and struct types.

The reference types include string arrays, classes, and interfaces.

Struct:

You can have fields and methods.

Initial values cannot be assigned to fields during definition.

No new is required. After declaring a structure object, you must assign an initial value to the structure member.

Unpacking and packing

Example:

Int a = 1;

Object o = I; // boxed

Int j = (int) o; // unpack

Value Transfer and reference Transfer

When the reference type is used as a parameter:
1. When modifying the variable itself, the result is similar to passing the value, that is, the value of the variable before the passing is not changed.
2. When modifying the attributes or fields of a variable, it is passed by reference, which will affect the value of the variable before transmission.
3. When the ref parameter is used, it is the actual reference transfer. Whether the variable itself is modified or the attribute or field of the variable is modified, the value of the variable before transmission will be affected.

Value Transfer: transfers the object's value copy. (That is, the parameter object in the function is a copy of the object in the stack of the object passed during the call .)
Reference transfer: transmits the address of the object in the stack. (That is, the parameter object in the function and the object passed during the call are all objects in the same stack .)

3. Use a set to organize relevant data

System. CollectionsA namespace contains interfaces and classes that define a set of objects (such as lists, queues, bit arrays, hash tables, and dictionaries.
System. Collections. GenericA namespace contains interfaces and classes that define a generic set. A generic set allows you to create a strongly typed set. It provides better type security and performance than a non-generic strong set.
System. Collections. SpecializedA namespace contains specialized and strong collections, such as list dictionaries for links, bitvectors, and collections that only contain strings.

Using System; using System. collections. generic; using System. text; using System. collections; namespace ConsoleApplication1 {class Program {static void Main (string [] args) {ArrayList list = new ArrayList (); Console. writeLine ("Traversal method 1:"); foreach (int item in list) // do not forcibly convert {Console. writeLine (item); // Traversal method 1} Console. writeLine ("Traversal method 2:"); for (int I = 0; I <list. count; I ++) // The array is length {int number = (int) list [I]; // The Console must be forcibly converted. writeLine (number); // Traversal method 2 }}}}

1. Brief description of the hash table (Hashtable)
In. in the. NET Framework, Hashtable is System. A container provided by the Collections namespace is used to process and present key-value pairs similar to key/value. The key is usually used for quick search, and the key is case sensitive; value is used to store the value corresponding to the key. In Hashtable, key/value pairs are of the object type, so Hashtable can support any type of key/value pairs.

2. Simple operations on Hash Tables
Add a key/value pair in the hash table: HashtableObject. Add (key, value );
Remove a key/value pair in the hash table: HashtableObject. Remove (key );
Remove all elements from the hash table: HashtableObject. Clear ();
Determine whether the hash table Contains the specified key: HashtableObject. Contains (key );
Traversal

Foreach (DictionaryEntry item in Hashtable)

{

Item. Key;

Item. Values;

}

The most common use of generics is the generic set, namespace System. collections. generic contains some Generic-based collection classes. Using Generic collection classes can provide higher type security and higher performance, avoiding repeated packing and unpacking of non-Generic sets.
Many non-generic collection classes have their corresponding generic collection classes. Below are common non-generic collection classes and their generic collection classes:
Non-generic collection class Generic collection class
ArrayList List <T>
HashTable DIctionary <T>
Queue Queue <T>
Stack Stack <T>
SortedList SortedList <T>
Single-Column generic set: List <Object> list = new List <Oibject> (); traverse for (int I = 0; I <list. count; I ++) {Console. writeLine (list [I]);} foreach (Object item in list) {Console. writeLine (item);} double-row generic set Dictionary <object, object> dic = new Dictionary <object, object> (); traverse foreach (KeyValuePair <object, object> item in dic) {Console. writeLine (dic. key); Console. writeLine (dic. value);} foreach (Object item in dic. keys) {Console. writeLine (item); Conso Le. writeLine (dic [item]. value);} foreach (Object item in dic. values) {Console. writeLine (item);} 4. in-depth method 1. constructor (1 .) the method name is the same as the class name (2 .) no return value (3 .) initialize the object. class Name () {// method body} 3. class Name (parameter list) {// method body} the implicit constructor system automatically assigns a non-argument constructor to the class. The overload of constructor without parameters and the construction with parameters can be considered as method overload. Method overload (1 .) same method name (2 .) different method parameter types or different number of parameters (3 .) in the same class, object interaction each class has its own features and functions. We encapsulate them as attributes and methods. Objects interact with each other through attributes and methods. It can be considered that the parameters of the method and the return values of the method are all messages transmitted between objects. In my personal understanding, why is it called interaction between objects. Because it is the property method interaction between objects. The inherited multi-state interface between classes. 6. Overview of inheritance and polymorphism 1. What Is Inheritance (1) Remove redundant code of classes (2) integration concept 2. base keyword and protected Modifier
Public class Person {protected string ssn = "111-222-333-444"; protected string name = "Zhang San"; public virtual void GetInfo () {Console. writeLine ("name: {0}", name); Console. writeLine ("No.: {0}", ssn) ;}} class Employee: Person {public string id = "ABC567EFG23267"; public override void GetInfo () {// call the GetInfo method of the base class: base. getInfo (); Console. writeLine ("member ID: {0}", id );}}

  

3. Subclass Constructor (1) implicitly calling the parent class Constructor (2) it is shown that the inheritance, encapsulation, and Polymorphism of calling the parent class constructor inheritance are an important feature of object-oriented programming.
The inherited class of its members is also called a base class or a parent class, and the class that inherits its members is also called a derived class or a subclass.
The derived class implicitly obtains all the members of the base class except the constructor and destructor. A derived class can only have one direct base class, so C # does not support multiple inheritance, but a base class can have multiple direct Derived classes.
Inheritance can be passed.
Private Members have actually been inherited, However, they cannot be accessed, because private members can only be accessed by declared classes or struct, so it seems that they are not inherited.

 If the base class contains a constructor without parameters

If the base class contains a constructor without parameters, you can customize the constructor with parameters in the derived class. If the base class defines a constructor with parameters, the constructor must be executed and implemented in the derived class. In this case, we can use the base keyword.  If the base class of a derived class is also a derived class, each derived class only needs to be responsible for the construction of its direct base class, not for the construction of indirect base class,
The execution sequence of the constructor starts from the top base class until the end of the last derived class.
Is a application Because both SE and PM inherit the Employee ID, that is, SE is a Employee, and PM is a Employee. Inherited value: (1) Integration simulates real-world relationships. oop emphasizes that everything is object, which is in line with our way of thinking about object-oriented programming. (2) Inheritance achieves code reuse (3) integration to make the program structure clearer Polymorphism Solve integration problems Virtual Method The virtual method is called the virtual method. The virtual method has a method body. Syntax Access modifier virtual return type method name () { // Method body } Rewrite Virtual Methods Access modifier override return value type method name () { // Method body } What is polymorphism Polymorphism literally means "multiple forms". It means that different interpretations can be made when the same operation acts on different objects to produce different results. Implementation of Polymorphism (1) method Rewriting (2) define parent Variables 7. deep understanding of Polymorphism

Rys replacement principle

In a software system, subclass can replace the location where the parent class appears, without affecting the functionality of the software.

Subclass can extend the features of the parent class, but cannot change the original features of the parent class. Sub-classes can implement abstract methods of parent classes, but cannot overwrite non-Abstract methods of parent classes. Sub-classes can add their own unique methods.

When the method of the subclass reloads the method of the parent class, the pre-condition (that is, the parameter of the method) of the method is looser than the input parameter of the parent class method.

When the subclass method implements the abstract method of the parent class, the post-condition (that is, the return value of the method) of the method is stricter than that of the parent class.

It looks incredible, because we will find that in our own programming, we often violate the Lee's replacement principle, and the program runs well. So everyone will have such questions. What would happen if I did not follow the Lee's replacement principle?

The consequence is that the chances of problems with the code you write will be greatly increased.

Example: Father son = new Son ();

In C #, there are two keywords that can be used to extract the truth replacement principle: the is and as operators are used to check whether the object and the specified type are compatible. The as operator is mainly used for type conversion between two objects.

The parent class is used as a parameter.

Example: First give several classes: 1. parent class, transportation tools class. There are a bunch of properties. 2. The automobile class inherits the parent class. 3. The subway class inherits the parent class. 4. Employees. Employees go home by transportation! The employee's way home. The parameter is a parent object!

Create a set of employees and initialize the employees. Just like =, you can use the items in the set to point out the way employees go home and pass a subclass! In summary, this is the subclass pointing to the parent class! This is also the so-called Lee's replacement principle!

Abstract classes and abstract methods

If a class is not associated with a specific thing, but only expresses an abstract concept, it is only used as a base class of its derived class. Such a class is an abstract class, when declaring a method in an abstract class, adding abstract is an abstract method.

Main differences between abstract classes and non-abstract classes:

· Abstract classes cannot be directly instantiated.

· Abstract classes can contain abstract members, but not abstract classes.

· Abstract classes cannot be sealed or static

An abstract method is an unimplemented method. You can declare an abstract method by adding a keyword abstract when defining a method.

Abstract method syntax

Access modifier abstract return value type method name ();

Note: The abstract method does not have closed braces, but is directly followed by ";" that is, it does not include the method body of the method execution logic!

Definition of abstract classes

Syntax: access modifier abstract class name {}

Note: abstract classes provide abstract methods. These methods are only defined. How to implement them is done by Non-Abstract subclasses of abstract classes.

Application of abstract classes and abstract methods

How to implement a subclass derived from an abstract parent class when an abstract parent class is derived from an abstract Child class, the Child class inherits all the features of the parent class, including the abstract methods it has not implemented. An abstract method must be implemented in a subclass unless its subclass is also an abstract class.

Application of abstract classes and abstract methods

How to implement a subclass derived from an abstract parent class when an abstract parent class is derived from an abstract Child class, the Child class inherits all the features of the parent class, including the abstract methods it has not implemented. An abstract method must be implemented in a subclass unless its subclass is also an abstract class.

Three features of Objects

Encapsulation: ensures the integrity and security of the object's data.

Inheritance: establishes relationships between classes to achieve code reuse, facilitating system expansion.

Polymorphism: different implementation methods can be implemented for the same method call.

8. scalable markup language XML

1. What is XML? What is the role?

L XML (eXtensible Markup Language) is an eXtensible Markup Language. The scalability is relative to HTML. Because the XML tag is not predefined, You need to define the tag.

L XML is designed to represent data rather than displaying data.

XML operations

Parse XMl files

Public static void Main (string [] args)

{

XmlDocument doc = new XmlDocument ():

Doc. Load ("Engineer. xml ");

XmlNode root = new XmlNode ();

Foreach (XmlNode item in doc. ChildNodes)

{

Switch (node. Name)

{

Case "id ":

Console. WriteLine (node. InnerText );

Break;

}

}

}

Use TreeView to display data
XmlDocument doc = new XmlDocument (); doc. load ("Beijing TV. xml "); XmlNode root = doc. documentElement; // find the root node foreach (XmlNode item in root. childNodes) // traverse the subnode {if (item. name. equals ("tvProgramTable") // determine whether the node Name is "tvProgramTable" {foreach (XmlNode items in item. childNodes) // traverse the subnode {TvProgram Tp = new TvProgram (); Tp. playTime = Convert. toDateTime (items ["playTime"]. innerText); Tp. meridien = items ["meridien"]. innerText; Tp. path = items ["path"]. innerText; Tp. programName = items ["programName"]. innerText; ProgramList1.Add (Tp); // bind to the set} TreeNode minenode = new TreeNode (); minenode. text = "my TV station"; // bind tvList. nodes. add (minenode); // The root node TreeNode root = new TreeNode (); root. text = "all TV stations"; // bind tvList. nodes. add (root); ChannelManager manager = new ChannelManager (); manager. resolveChannels (); List <ChannelBase> list = manager. list; foreach (ChannelBase item in list) {TreeNode tn = new TreeNode (); tn. text = item. channelName; tn. tag = item; root. nodes. add (tn );}

9. File Operations

How to read and write files

Introduce using. System. IO;

String path = txtFilePath. Text;

String content = txtContent. Text;

If (path. Equals (null) | path. Equals (""))

{

MessageBox. Show ("the file path is not blank ");

Return;

}

Try

{

FileStream nyfs = new FileStream (path, FileMode. Create)

StreamWriter mysw = new StreamWriter (myfs );

Mysw. Write (content );

Mysw. Close ();

Myfs. Close ();

MessageBox. Show ("successfully written ");

}

Catch (Exception ex)

{

MessageBox. Show (ex. Message );

}

File stream

Syntax

FileStream file stream object = new FileStream (string filePath, FileMode fileMode );

File Reader

StreamWriter

StreamWriter mysw = new StreamWriter ();

Solve the garbled Problem

FileStream myfs = new FileStream (path, FileMode. Open)

StreamReader mySr = new StreamReader (myfs, Encoding. Default );

Content = mySr. ReadToEnd ();

TxtContent. Text = content;

File and directory operations

File and Directory

Common File Methods

Exites (string path) is used to check whether the specified file exists.

Copy () copy an object

Move () Move a file

Delete () Delete an object

Sample small Resource Manager

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace Chap09 _ Resource Manager {class MyFile {public string FileName {get; set;} public long FileLength {get; set;} public string FileType {get; set ;} public string FilePath {get; set ;}} using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms; using System. IO; using System. diagnostics; namespace Chap09 _ Resource Manager {public partial class FrmMain: Form {public FrmMain () {InitializeComponent ();} private void FrmMain_Load (object sender, EventArgs e) {DriveInfo [] di = DriveInfo. getDrives (); foreach (DriveInfo item in di) {TreeNode tn = new TreeNode (item. name); tn. tag = item. name; tvList. nodes. add (tn) ;}} public void BindInfo (TreeNode node) {try {lvList. items. clear (); DirectoryInfo dir = new DirectoryInfo (node. tag. toString (); DirectoryInfo [] dirs = dir. getDirectories (); foreach (DirectoryInfo item in dirs) {TreeNode tn = new TreeNode (); tn. text = item. name; tn. tag = item. fullName; node. nodes. add (tn);} FileInfo [] fi = dir. getFiles (); List <MyFile> files = new List <MyFile> (); foreach (FileInfo item in fi) {MyFile mf = new MyFile (); mf. fileName = item. name; mf. fileLength = item. length; mf. fileType = item. extension; mf. filePath = item. fullName; files. add (mf);} foreach (MyFile item in files) {ListViewItem items = new ListViewItem (item. fileName); items. subItems. add (item. fileLength. toString (); items. subItems. add (item. fileType); items. subItems. add (item. filePath); lvList. items. add (items) ;}} catch (Exception ex) {MessageBox. show ("" + ex. message) ;}} private void tvList_AfterSelect (object sender, TreeViewEventArgs e) {TreeNode node = this. tvList. selectedNode; this. bindInfo (node);} private void lvList_DoubleClick (object sender, EventArgs e) {Process. start (lvList. selectedItems [0]. subItems [3]. text);} private void copy ToolStripMenuItem_Click (object sender, EventArgs e) {FolderBrowserDialog fbd = new FolderBrowserDialog (); DialogResult result = fbd. showDialog (); string sourcePath = lvList. selectedItems [0]. subItems [3]. text; string desPath = null; if (result = DialogResult. OK) {desPath = fbd. selectedPath; desPath + = "\" + lvList. selectedItems [0]. subItems [0]. text; File. copy (sourcePath, desPath); MessageBox. show ("copied") ;}} private void Delete ToolStripMenuItem_Click (object sender, EventArgs e) {lvList. items. clear (); string deletePath = lvList. selectedItems [0]. subItems [3]. text; File. delete (deletePath); MessageBox. show ("deleted successfully ");}}}

  

 

After startup, you can read the things on your hard disk!

Well, you can write it here. Summary.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.