Presentation is loading. Please wait.

Presentation is loading. Please wait.

Decisions, repetition, Code Snippets, Comments, and Intellisense

Similar presentations


Presentation on theme: "Decisions, repetition, Code Snippets, Comments, and Intellisense"— Presentation transcript:

1 Decisions, repetition, Code Snippets, Comments, and Intellisense
Additional C# Information Decisions, repetition, Code Snippets, Comments, and Intellisense Decisions, Repetition, Code Snippets, Comments, and Intellisense November 16, 2018

2 Topics Decisions and Repetition Using Code Snippets
Comments and Intellisense Decisions, Repetition, Code Snippets, Comments, and Intellisense November 16, 2018

3 Decisions and Repetition
Similar to Java and C++ Decisions, Repetition, Code Snippets, Comments, and Intellisense November 16, 2018

4 Decisions and Repetition
if, switch, while, for Decisions, Repetition, Code Snippets, Comments, and Intellisense November 16, 2018

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 November 16, 2018

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

7 switch-case The switch statement is similar to Java
Each case MUST end in a break, return, or throw May not “fall through” to the next case Exception: empty cases are allowed, and they fall through to the next non-empty case The exceptions noted above allow for several cases to have same action switch expression and case labels must be integer, char, enum, or string type Decisions, Repetition, Code Snippets, Comments, and Intellisense November 16, 2018

8 switch-case Decisions, Repetition, Code Snippets, Comments, and Intellisense November 16, 2018

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 November 16, 2018

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 November 16, 2018

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 November 16, 2018

12 Code Snippets Using code snippets saves both time and keystrokes
Code snippets are customizable segments of code that are easily inserted when needed They relieve the programmer from typing the skeleton code 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 November 16, 2018

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 Insert Snippet tool Decisions, Repetition, Code Snippets, Comments, and Intellisense November 16, 2018

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

15 Using Snippets The names of common code snippets 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 November 16, 2018

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 November 16, 2018

17 Overriding an Inherited Method
To override an inherited method, one may type override and press space key to 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 November 16, 2018

18 Email selected snippet
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 a selected snippet to a colleague or team member selected snippet Decisions, Repetition, Code Snippets, Comments, and Intellisense November 16, 2018

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 regular expressions 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 November 16, 2018

20 Using Pascal Casing with Intellisense
In addition to the “contains” approach Intellisense uses, it also takes advantage of the Pascal Casing 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 November 16, 2018

21 Comments in C# Documentation and Intellisense
Decisions, Repetition, Code Snippets, Comments, and Intellisense November 16, 2018

22 Ordinary Comments in C#
Ordinary comments in C# have the same formats as in Java A single or partial line comment begins with // A multiline block of comments may consist of Several lines 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 November 16, 2018

23 Examples: Ordinary Comments in C#
Decisions, Repetition, Code Snippets, Comments, and Intellisense November 16, 2018

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 XML file that may be formatted into a user’s manual or programmer’s guide, 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 November 16, 2018

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 XML comments for you Decisions, Repetition, Code Snippets, Comments, and Intellisense November 16, 2018

26 Example Type /// here Decisions, Repetition, Code Snippets, Comments, and Intellisense November 16, 2018

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

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

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 November 16, 2018

30 From the XML <param> comment we wrote
Example, cont. When writing code that uses this constructor, Intellisense updates as we type to show what is next, using the XML comments we wrote Where we are in typing From the XML <param> comment we wrote Decisions, Repetition, Code Snippets, Comments, and Intellisense November 16, 2018

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 November 16, 2018


Download ppt "Decisions, repetition, Code Snippets, Comments, and Intellisense"

Similar presentations


Ads by Google