In this section:
1, analysisHello,WorldProgram
1.1 Initial class(class)and namespace(namespace)
2, class library reference
2.1DLLquotation(Black box reference)
2.2Project reference(White box reference)
2.3Create your own Class library project
3, dependencies
span>
4, eliminate errors
1< span style="font-family: Song Ti;">, analysisHello,WorldProgram
1.1、Class (class)constitutes the main body of the program< /p>
1.2, namespace(namespace)Organize classes in a tree structure(and other types)
e.g.ButtonandPathClass
Namespace can effectively avoid conflicts between classes with the same name.
C# is a completely object-oriented language, the entire program is contained in Inside a class:class Program{}
When a namespace is introduced:Using….is equivalent to telling the program if I When writing a class, if I did not mark the namespace of the class before the class name, I would search it in the namespace I referenced, and see which namespace contains this class, and use the class in this namespace That’s it.
For example:ConsoleThere are two ways to quote classes:
The first type: no namespace is introduced:System.Console.WriteLine();
If you want to use itSystemThe classes in it must be the same as above every time Prefix the class(This is called permission naming), it seems redundant.
Can I write it? ConsoleThe compiler knows it is fromSystemWhere is it from here?
The second type: introduce namespace:
Using System;
Console.WriteLine();quote laterSystem The classes in the namespace do not need to be prefixed. This is the convenience of quoting the namespace.
*Tips: find a certain category Namespaces. Two methods
The first method is to know through the document, open the imported local help document, and search for the kind.
The second: relatively simple method: Put the mouse on this category and a blue smart mark will appear in the lower left corner. There are two options:Using SystemandSystem.Consolerespectively indicate two methods of referencing a namespace. (The compiler is more clever to guess which namespace this class belongs to)
Be careful not to name everything conveniently Space is quoted in, conflicts will occur. Because the namespace organizes classes in a tree structure,
Different tree structures may also have the same classes. This If you use the method of introducing namespaces to reference related classes, it is easy to cause confusion; for example,
PathThe class belongs to bothSystem.IOThe namespace also belongs to System namespace, then you don’t know which namespace to reference Path class now.
At this time, the first method that does not introduce namespaces should be used to accurately reference the same name in different namespaces the type.
such as:System.PathorSystem.IO.PathJust You can clearly distinguish classes with the same name in different namespaces.
* Another exampleButtonThere are more than ten types. At this time, the importance of namespace is highlighted. If there is no name There can only be one space.Buttontype, Effectively resolve conflicts of class names.
*This also gives us a design idea, When we are designing a class, we must give the class an accurate name and place it very accurately in the namespace where it should be. This is convenient for writing programs by ourselves and for others to use our programs quickly and quickly. Find our class comfortably. For example: I created a .hfile, and not only need to quote it elsewhere Introduce the header file.hAlso introduce the namespace of this custom class, Make him unique.
2, the use of class library
It’s like a book. Although you know which library the book belongs to, can you take the book out if you don’t even use the library?
So there are: classes and namespaces are placed in a place called class libraries, and class libraries are the physical basis of classes and namespaces
(Different technical types of projects will refer to different classes by default Library)
2.1、DLL(Class library )(Dynamic link library) Dynamic link library, reference (black box reference, no source code)*NuGetIntroduction. span>
If you want to use the corresponding namespace and class, you must first quote the corresponding Class library. Where can I find a class library? Solution->quote,The inside are all the class libraries referenced by different types of programs in your new creation by default. Double-click one of the class libraries, and “Object Browser” will pop up.window, one layer of which lists class libraries->Name spaces contained in various libraries->The classes contained in the relevant namespace.
*emphasize one point: the essence of different project templates After the project was built, different technologies were quoted accordingly (such aswinformand< /span>WPF) required class library.
What if we want to reference other libraries besides the library referenced by the template? There are two ways to reference the class library in daily work:
The first type is to directly quote the compiled DLL. This kind of quote is called black box quote, because we don’t have DLLsource code;
The second type: we have the source code of the class library, and then we reference the source code, the source code is placed in the project, this is called project reference, or white box reference, you can see To the source code.
The two types of references have their own characteristics. It should be noted that when you bring the DLL after you bring it, you must with a description document , Otherwise it’s not very useful, because the black box reference is not clear if you don’t know the source code.DLL what namespace and class can’t be used. Where to add the class library? In the solution->quote->right click-> Add reference->Bottom right corner “Browse”Add local DLL, so download it The class library references the program, and then you can use the class library against the documentation. (You can also use the above method to check how many namespaces and related classes this class library has)
*< span style="font-family: 宋体;">There are problems with black box quoting. If the code has errors and cannot be modified, you can also tell the person who made the class library to change it and compile it againDLLSend it to me again. On the other hand, my program has a dependency on the introduced class library, that is, there is a problem with its class library. My program will also have problems. This is a dependency, and there will be a dependency at the class level, that is, I Class: such asC#The program body is included inclass Program, the class of the library is called, and there is a dependency between the two classes. Even as one, one side is wrong, and both sides are wrong. *Be careful of this dependency when writing programs. This dependency is probably 5,6Some kinds of dependencies are very strong, Some dependencies are very weak. We try to use weaker dependencies. In this way, my program will not have problems with a certain underlying class. All of my upper-level programs cannot work.
One of the solutions is weak dependency : For example, you can temporarily replace the wrong DLL with a similar class, and then continue to modify the program, and wait for theDLL modify it before continuing to use the library. How to arrange?This is more advanced content.
Can be foundDLLThe document isMSDNdocument The epitome of:MSDNThere is a very large library of documents in it You can find information about each class, including methods, properties, events, and so on.
Idea: Originally the console program cannot be displayed Window, but if you introduce related classes, it becomes possible to display windows. Solution->quote->Right click to add a reference->Assembly options Calibri searchSystem.Windows.Forms,Add this library, then The class library is part of our program, you can freely quote, quoteDLLclass Before, as mentioned above, you must write the namespace first, and the way conflicts:Using System.Windows.Forms;Re-quotingFormThe class is no problem NS.
Using trendy technology(NuGet )To solve the more complex dependency problem, the program and the class library have certain dependencies, and the lower-level library cannot reference the upper-level library. Because the upper class library needs the support of many lower class libraries to run, the upper class library cannot be used unless a lower class library is not referenced. When quoting, there is a class library with the same name or a different version in the hard disk. This quoting is likely to introduce some very difficult errors in the program. Because at this time you only have DLL, no source code, it can almost be said to be ” Blindfolded references to class libraries” This is very dangerous! Especially for large projects.
Solution: use a class library package The form is placed on the Internet, and the customer wants to use me to type a command and a group of class libraries are included in the project. There is no need to manually add reference class libraries. This is very safe and efficient. This is NuGetTechnology: Solutions->quote ->ManagementNuGet program package, search for a package of related class libraries here, install it, it will be automatically included in the references of our project Add related class libraries, and the newly added class libraries are all created by NuGet Don’t worry about management, the version, the lack of a certain library and other issues, is very convenient. *Summary: UseNuGet to quote some very good libraries on the Internet.
2.2, project reference (white box reference, Source code)
When writing classes by yourself There is a namespace to make it easier for others to reference. A project belongs to multiple solution(Solution) This is the reuse of the project. When we have the source code of someone else’s library, we can right-click on the solution ->Add->There is an item, so that you can add other people’s project files to our program, right-click on the reference< /span>->Add, you can see the others we added in the solution tab The class library in the project is now, and you can start to reference the class library of the project by selecting Add. It can be seen that different projects under the same solution can use each other’s class libraries through the above method.
Debug repaired, troubleshooting YesRoot cause! Do not patch on the patch, not fixing the root cause of the problem will only get worse.
This is the white box reference.
2.3, create your own class library project< /span>
right-click solution->Add->New item span>->select class library(class library)Project,class library It is not an executable file. The compiled result isDLL(class library), in the generatedCSIn the file, compile the related classes and methods of the class library. Right-click on the created class library project->The generation will be Project local source code folderbin->debuggenerated in the folderDLL(class library file) so that it can be distributed by attaching instructions for use.
3, dependencies
There is a dependency relationship between classes and classes, and there is also a dependency relationship between class libraries and class libraries. If there is a problem with the underlying class or class library, then the upper class or class library will not work. Normal, so the dependency relationship (also called the coupling relationship) is very important.
3.1, the coupling relationship between classes (or objects)< /span>
3.2, the pursuit of excellent program “High cohesion, low coupling” (teaching programs often violate this principle)
High cohesion: refers to some data and some functions Put it in which classes it belongs to, and put it in these classes accurately. This is called high cohesion;
Low coupling: The relationship between the class and the class as low as possible, as loose as possible, is called low coupling.
The design of the class library is also based on this principle. Some classes should be placed in which class library, and don’t Random placement, the relationship between the class library and the class library should also be as low as possible, low-coupling, and low-dependency. This way the program structure will be very clear.
3.3、UML(Universal Modeling Language) class diagram:
Show the relationship between classes and classes.
4, eliminate errors
4.1 , carefully read the compiler’s error report.
4.2、MSDNCombine documents with search engines.
When you see a large error There may not be so many errors. Sometimes a small error causes so many errors, such as Chinese punctuation. (Actually, the compiler has a certain level of intelligence. If the statement is correct, it will be highlighted); the error that is really difficult to eliminate is that the compiler thinks it is correct, but it will go wrong when it runs. This is a logical error and requires step-by-step debugging. to solve.