Download presentation
Presentation is loading. Please wait.
Published byΦιλομήλ Τοκατλίδης Modified over 6 years ago
1
Code Snippets, Intellisense, Comments, Type Conversion
Additional C# Information Code Snippets, Intellisense, Comments, Type Conversion Decisions, Repetition, Code Snippets, Comments, and Intellisense November 16, 2018
2
Topics Regions Using Code Snippets Comments and Intellisense
Data Type Conversion Decisions, Repetition, Code Snippets, Comments, and Intellisense November 16, 2018
3
Code Outlining Another feature that aids developers in navigating large code files is code outlining Regions of code may be designated Each region may be collapsed into a single line in the code editor Collapsed regions may be expanded to reveal their detail whenever it is needed Some regions are “automatically” designated for you including Method definitions XML comment blocks Classes Namespaces and more Odds and Ends November 16, 2018
4
Code Outlining Region designators Region designators Odds and Ends
November 16, 2018
5
Code Outlining Clicking on one of the minus signs will collapse the associated region and replace the minus with a plus (for later expansion) Note that the namespace and class may also be collapsed Each collapsed region becomes a single line Odds and Ends November 16, 2018
6
Designating Other Regions
Developers may use #region and #endregion preprocessor directives to mark beginning and end of a region not already designated by Visual Studio For example, in a large class, there may be several overloaded constructor methods (the String class has about 8 overloaded constructor methods) One may use the #region … #endregion directives to designate all of them as one large region One can then collapse or expand all constructors at once Odds and Ends November 16, 2018
7
#region … #endregion The syntax of these preprocessor commands is
#region Title Phrase . . . #endregion The #region and #endregion commands must be paired Regions may be nested but not overlapping The Title Phrase will be displayed when the region is collapsed One is not limited to using regions that correspond to large blocks of code such as methods or classes One may use these commands anywhere within reason – including turning a logical segment of a method into its own region One may use the Surrounds With … capability discussed earlier to embed a selected existing segment of code with #region … #endregion Odds and Ends November 16, 2018
8
Can be collapsed to this small segment
Example Three new regions added, each of which has two or three regions already collapsed inside it Can be collapsed to this small segment Odds and Ends 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 Collapsed region 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 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 code for 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 VS, 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 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
Can Generate User’s Manual
CodeRush Documentor Plugin Decisions, Repetition, Code Snippets, Comments, and Intellisense November 16, 2018
32
Two places after the decimal in this output
Another Example Another tag Two places after the decimal in this output Decisions, Repetition, Code Snippets, Comments, and Intellisense November 16, 2018
33
XML Documentation Tags
See Visual Studio “Help” for information on each tag Decisions, Repetition, Code Snippets, Comments, and Intellisense November 16, 2018
34
Type conversion Casting and conversion methods Odds and Ends
November 16, 2018
35
Data Types and Conversions
Two items may be of compatible or incompatible types An integer is compatible with a double because both are numeric and an integer can be converted into a double without loss of data “Matt” is a string that cannot be converted into a double in any intuitive way; the two types are not compatible Odds and Ends November 16, 2018
36
Implicit/Explicit Conversions
For compatible types, there may or may not be implicit conversions from one type to another Depends on whether the compiler knows how to make the conversion Depends on whether there is a potential for loss of data Example, the following produces an implicit conversion from the integer on the right-hand-side to the double on the left. No data loss is possible If there is a possibility of data loss, an explicit operation is required: the programmer must ask for a conversion Odds and Ends November 16, 2018
37
Explicit Conversions One way to request a conversion is to cast between compatible types A cast operation may be an acknowledgement of possible data loss and “it is OK with me” May be a statement that “I know the object is really of this other type and it is OK to treat it that way” Casting as string means we can use string methods and string properties Odds and Ends November 16, 2018
38
Casting The syntax for requesting a cast is (new type) item
Item must be of a type compatible with the requested type so that a conversion is possible If, at run time, it is determined that one cannot make the requested conversion, an exception is thrown For example, an array of Object may contain any types of items such as strings, doubles, PigLatin objects, Employee objects, etc. If we try to cast and use one of the items as a string, that will work only if that particular item is a string rather than one of the other types of items; it will fail for a double or an Employee object. Odds and Ends November 16, 2018
39
Casting Casting is a “temporary” operation
It does not permanently change the type of data being cast Internally, C# makes a temporary variable of the converted type and converts the item being cast into the temporary variable The value in the temporary is used for the rest of the operation – at which time it ceases to exist Odds and Ends November 16, 2018
40
Casting An object of a derived class isa object of its base class (an Employee isa Person), but the reverse is not true (some Persons are not Employees) Casting is required when an object is defined to be of a base type, but it actually refers to a derived type of object To use the methods and properties of the derived type, one must first cast the object to that type The casting is OK because the object really is of the derived type See the Object array example several slides back Odds and Ends November 16, 2018
41
Using “as” to Cast to a Different Type
Exception-free “Casting” approach
42
Check to see if the as cast was successful
The as Keyword The as operator provides a type of casting operation Ordinary cast operations throw an exception if the cast is not valid If the conversion is not possible, as returns null instead of raising an exception Use of the as keyword Check to see if the as cast was successful
43
Was the as cast successful?
Another as Example Trying to cast as a string. It works on those items that are strings, but returns null otherwise Was the as cast successful? Results
44
Other Explicit Conversions
Casting can only be used in cases in which the types are compatible and the compiler knows how to do the conversion There are cases in which a conversion is needed but a cast cannot be done. Example: To input a salary from the keyboard, one may use Console.ReadLine ( ); This inputs a string, hopefully the string representation of a number such as “ ” We cannot cast this string as a Decimal or a Double however since they are incompatible types Odds and Ends November 16, 2018
45
Explicit Conversions Explicit Conversions that cannot be done by casting require the programmer to invoke a method to do the conversion Example 1: char [ ] cArray = strVowels.ToCharArray ( ); is used to convert a string into an array of characters Example 2: string strLine = MyTrip.ToString ( ); may convert a decimal MilesPerGallon object to a string Any class may have methods that convert objects of some other type to its type or vice versa Odds and Ends November 16, 2018
46
String to Numeric Conversions
The standard .NET numeric data types such as Int32, Double, Decimal, and so forth have static methods named Parse Each takes a string argument – the string representation of a number of that type Method converts the value argument to the appropriate numeric type Example: Throws exception if the string cannot be parsed as the specified type Odds and Ends November 16, 2018
47
String Conversions To avoid exceptions in cases where the Parse argument is invalid, use TryParse public static bool TryParse ( string s, out double result ) Works similar to Parse, but returns true or false to indicate whether it was successful If it was successful, the second argument has the result in it out specifies that the argument need not have a value going in because the method will assign one to be returned Odds and Ends November 16, 2018
48
Example: String Conversions
Odds and Ends November 16, 2018
49
Conversion between numeric types
Conversion from one numeric type to another is typically done implicitly (if appropriate) or by casting (if needed) However, .NET numeric structs have conversion methods Example: Decimal has the following methods Odds and Ends November 16, 2018
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.