1 / 31

Decisions , repetition, Code Snippets, Comments, and Intellisense

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.

cain
Download Presentation

Decisions , repetition, Code Snippets, Comments, and Intellisense

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Additional C# Information Decisions, repetition, Code Snippets, Comments, and Intellisense Decisions, Repetition, Code Snippets, Comments, and Intellisense

  2. Topics • Decisions and Repetition • Using Code Snippets • Comments and Intellisense Decisions, Repetition, Code Snippets, Comments, and Intellisense

  3. Decisions and Repetition Similar to Java and C++ Decisions, Repetition, Code Snippets, Comments, and Intellisense

  4. Decisions and Repetition if, switch, while, for Decisions, Repetition, Code Snippets, Comments, and Intellisense

  5. 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

  6. Counting Loop - For May initialize and increment/decrement more than one counter Program Output Decisions, Repetition, Code Snippets, Comments, and Intellisense

  7. 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

  8. switch-case Decisions, Repetition, Code Snippets, Comments, and Intellisense

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. Snippet • A snippet is a tailorable set of lines of code to do a common task Decisions, Repetition, Code Snippets, Comments, and Intellisense

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. Comments in C# Documentation and Intellisense Decisions, Repetition, Code Snippets, Comments, and Intellisense

  22. 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

  23. Examples: Ordinary Comments in C# Decisions, Repetition, Code Snippets, Comments, and Intellisense

  24. 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

  25. 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

  26. Example Type /// here Decisions, Repetition, Code Snippets, Comments, and Intellisense

  27. Example, continued This skeleton of XML comments is generated instantly Decisions, Repetition, Code Snippets, Comments, and Intellisense

  28. Example, cont. Fill in details as shown Decisions, Repetition, Code Snippets, Comments, and Intellisense

  29. 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

  30. 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

  31. 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

More Related