Additional C# Information
DECISIONS, REPETITION, CODE
SNIPPETS, COMMENTS, AND
INTELLISENSE
Decisions, Repetition, Code Snippets, Comments, and Intellisense
October 3, 2015
1
Topics
 Decisions and Repetition
 Using Code Snippets
 Comments and Intellisense
Decisions, Repetition, Code Snippets, Comments, and Intellisense
October 3, 2015
2
Decisions and Repetition
Similar to Java and C++
Decisions, Repetition, Code Snippets, Comments, and Intellisense
October 3, 2015
3
Decisions and Repetition
if, switch, while, for
Decisions, Repetition, Code Snippets, Comments, and Intellisense
October 3, 2015
4
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
October 3, 2015
5
Counting Loop - For
May initialize and
increment/decrement
more than one counter
Program
Output
Decisions, Repetition, Code Snippets, Comments, and Intellisense
October 3, 2015
6
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
October 3, 2015
7
switch-case
Decisions, Repetition, Code Snippets, Comments, and Intellisense
October 3, 2015
8
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
October 3, 2015
9
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
October 3, 2015
10
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
October 3, 2015
11
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
October 3, 2015
12
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
October 3, 2015
13
Snippet
 A snippet is a tailorable set of lines of code to
do a common task
Decisions, Repetition, Code Snippets, Comments, and Intellisense
October 3, 2015
14
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
October 3, 2015
15
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
October 3, 2015
16
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
October 3, 2015
17
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
October 3, 2015
18
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
October 3, 2015
19
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
October 3, 2015
20
Comments in C#
Documentation and Intellisense
Decisions, Repetition, Code Snippets, Comments, and Intellisense
October 3, 2015
21
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
October 3, 2015
22
Examples: Ordinary Comments in C#
Decisions, Repetition, Code Snippets, Comments, and Intellisense
October 3, 2015
23
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
October 3, 2015
24
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
October 3, 2015
25
Example
Type /// here
Decisions, Repetition, Code Snippets, Comments, and Intellisense
October 3, 2015
26
Example, continued
This skeleton of XML
comments is generated
instantly
Decisions, Repetition, Code Snippets, Comments, and Intellisense
October 3, 2015
27
Example, cont.
Fill in details as
shown
Decisions, Repetition, Code Snippets, Comments, and Intellisense
October 3, 2015
28
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
October 3, 2015
29
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
October 3, 2015
30
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
October 3, 2015
31
Descargar

C# Lecture 3 – More Introduction to C# and VS