320 likes | 624 Views
Additional C# Information. Decisions , repetition, Code Snippets, Comments, and Intellisense. Topics. Decisions and Repetition Using Code Snippets Comments and Intellisense. Decisions and Repetition. Similar to Java and C++. Decisions and Repetition. if, switch, while, for.
E N D
Additional C# Information Decisions, repetition, Code Snippets, Comments, and Intellisense Decisions, Repetition, Code Snippets, Comments, and Intellisense
Topics • Decisions and Repetition • Using Code Snippets • Comments and Intellisense Decisions, Repetition, Code Snippets, Comments, and Intellisense
Decisions and Repetition Similar to Java and C++ Decisions, Repetition, Code Snippets, Comments, and Intellisense
Decisions and Repetition if, switch, while, for Decisions, Repetition, Code Snippets, Comments, and Intellisense
if – else, while, for • The if-else, while, for constructs in C# is almost identical to counterparts in Java Decisions, Repetition, Code Snippets, Comments, and Intellisense
Counting Loop - For May initialize and increment/decrement more than one counter Program Output Decisions, Repetition, Code Snippets, Comments, and Intellisense
switch-case • The switch statement is similar to Java • Each caseMUST end in a break, return, or throw • May not “fall through” to the next case • Exception: emptycases are allowed, and they fall through to the next non-empty case • The exceptions noted above allow for severalcases to have sameaction • switch expression and case labels must be integer, char, enum, or string type Decisions, Repetition, Code Snippets, Comments, and Intellisense
switch-case Decisions, Repetition, Code Snippets, Comments, and Intellisense
Surround with … and Snippets Visual Studio tools to help get the construct correct and do it more easily Decisions, Repetition, Code Snippets, Comments, and Intellisense
Surround With … • If one has existing code and discovers that some of it should be in a loop, an if-else statement, a try block, or some similar construct, VS can help convert to the desired state • Select the code to embed, and right-click on it • From the resulting menu, choose Surround With… Decisions, Repetition, Code Snippets, Comments, and Intellisense
Surround With …, continued • From the dropdown list, choose the desired structure Variable names are highlighted and selected, making it easy to change the names to suit your needs Decisions, Repetition, Code Snippets, Comments, and Intellisense
Code Snippets • Using code snippets saves both time and keystrokes • Codesnippets are customizablesegments of code that are easilyinserted when needed • They relieve the programmer from typing the skeletoncode in common situations such as for and while loops, try-catch-finally, foreach, class, enum, and so forth Decisions, Repetition, Code Snippets, Comments, and Intellisense
If you cannot remember … • If the exact format for the construct you need eludes you at the moment you need it, use VS’s InsertSnippet tool Decisions, Repetition, Code Snippets, Comments, and Intellisense
Snippet • A snippet is a tailorable set of lines of code to do a common task Decisions, Repetition, Code Snippets, Comments, and Intellisense
Using Snippets • The names of common codesnippets are closely related to the keywords involved • If one types the name of a snippet at the beginning of a line and then presses the tab key twice, the snippet is inserted as shown on the next slide Names of some snippets Decisions, Repetition, Code Snippets, Comments, and Intellisense
Using Snippets Continued If one types a snippet name such as for … Note: Intellisense helps … after pressing TAB twice, this code is inserted The variables are highlighted and selected for easy modification if desired Decisions, Repetition, Code Snippets, Comments, and Intellisense
Overriding an Inherited Method • To override an inherited method, one may type override and press space keyto get a list of all methods one may override from the current base class These are from System.Object, but the list depends on where the current class is in an inheritance hierarchy Decisions, Repetition, Code Snippets, Comments, and Intellisense
More on Snippets • One may also save a piece of code as a snippet, for later, repeated use • Use the Snippet Manager from the Tools menu • Can also drag/drop code to Toolbox for later use • One may also easily e-mail a selected snippet to a colleague or team member Email selected snippet Decisions, Repetition, Code Snippets, Comments, and Intellisense
Similar to Using Snippets … • Intellisense is a great help when typing • If one types the first few characters of a keyword or identifier name, Intellisense shows a list of all such words beginning with those characters and you can select the one you want (highlight it and press TAB) • In VS2010, Intellisense uses regularexpressions to find what you want using a “contains” approach Typing “mem” gives all choices containing mem such as OutOfMemoryException and MissingMemberException Decisions, Repetition, Code Snippets, Comments, and Intellisense
Using Pascal Casing with Intellisense • In addition to the “contains” approach Intellisense uses, it also takes advantage of the PascalCasing C# uses in names • Only type the UPPER CASE characters from the name • To insert the class name OutOfMemoryException in code, one only needs to type OOM and press TAB, for example Type OOM to get this Intellisense Decisions, Repetition, Code Snippets, Comments, and Intellisense
Comments in C# Documentation and Intellisense Decisions, Repetition, Code Snippets, Comments, and Intellisense
Ordinary Comments in C# • Ordinary comments in C# have the same formats as in Java • A single or partialline comment begins with // • A multilineblockof comments may consist of • Severallines of single line comments • Comments that begin with /* … and end at some later point with … */ • Used for same purposes as in other languages Decisions, Repetition, Code Snippets, Comments, and Intellisense
Examples: Ordinary Comments in C# Decisions, Repetition, Code Snippets, Comments, and Intellisense
XML Comments • XML comments are used in C# for two primary purposes • Build documentation in a standard way that can be extracted to an XMLfile that may be formatted into a user’smanual or programmer’sguide, somewhat similar to Javadoc • Generate documentation to be used by Intellisense within any program that uses the class Decisions, Repetition, Code Snippets, Comments, and Intellisense
XML Comments • XML comments have a common format • Begin with three forward slashes: /// • Use specific XML tags such as • <summary></summary> • <param name=“parameter_name"></param> • <returns></returns> • Designate specific items used by Intellisense • VS can auto-generate a skeleton of XMLcomments for you Decisions, Repetition, Code Snippets, Comments, and Intellisense
Example Type /// here Decisions, Repetition, Code Snippets, Comments, and Intellisense
Example, continued This skeleton of XML comments is generated instantly Decisions, Repetition, Code Snippets, Comments, and Intellisense
Example, cont. Fill in details as shown Decisions, Repetition, Code Snippets, Comments, and Intellisense
Example, cont. • Hover the cursor over the name of the constructor when it is used in code to see Intellisense Note that the second line is exactly the <summary> provided in the XML comment Decisions, Repetition, Code Snippets, Comments, and Intellisense
Example, cont. • When writing code that uses this constructor, Intellisenseupdatesaswetype to show what is next, using the XMLcomments we wrote Where we are in typing From the XML <param> comment we wrote Decisions, Repetition, Code Snippets, Comments, and Intellisense
Another Example Another tag; there are several other standard tags one may use Two places after the decimal in this output Decisions, Repetition, Code Snippets, Comments, and Intellisense