c# 3.0 course

Download C# 3.0 Course

If you can't read please download the document

Upload: amir-simantov

Post on 21-May-2015

700 views

Category:

Technology


7 download

DESCRIPTION

Practical Programming with C# 3.0Using Microsoft Visual Studio 2008 with .NET Framework 3.5FIVE DAYS COURSEGiven in Israel, Europe and Middle-East

TRANSCRIPT

  • 1. A Sample Snippet from
    • semanticase.com
  • Amir Simantov
  • Practical Programming with
  • C# 3.0
  • Using Microsoft Visual Studio 2008 with .NET Framework 3.5
  • Instructor-Led Course
  • VERSION 2008.10.01
  • Amir Simantov, Israel All Rights Reserved

-------------------------------------------------------- 2.

  • Just Because You Can Doesnt Mean You Should

-------------------------------------------------------- Join the course or order it from semanticase.com 3.

  • Advanced .NET Courses are also available atsemanticase.com
  • Including LINQ

-------------------------------------------------------- Join the course or order it from semanticase.com 4. Chapters

  • 1. Introducing C# and .NET See Free Sample
  • 2. CLR
  • 3. Procedural
  • 4. Types
  • 5. Strings
  • 6. Objects See Free Sample
  • 7. Strings
  • 8. Static Classes and Members

Practical Programming with C# 3.0 1 -------------------------------------------------------- Join the course or order it from semanticase.com 5. Chapters

  • 9. Inheritance
  • 10. Attributes
  • 11. Interfaces
  • 12. Disposing of Objects
  • 13. Code Integrity
  • 14. Arrays
  • 15. Collectioning
  • 16. Generic Collections

Practical Programming with C# 3.0 2 -------------------------------------------------------- Join the course or order it from semanticase.com 6. Chapters

  • 17. Functional De-coupling
  • 18. Querying Data Using Linq
  • 19. Lite Object Relation Mapping (ORM)
  • 20. APPENDIXES
      • Coding Guidelines
      • Visual Studio 2008
      • Learning Resources
  • See full syllabushere .

Practical Programming with C# 3.0 3 -------------------------------------------------------- Join the course or order it from semanticase.com 7.

  • Free Sample: CHAPTER 1
  • Managed code ahead
  • Introducing C# and .NET

Practical Programming with C# 3.01 -------------------------------------------------------- Join the course or order it from semanticase.com 8. Evolution of Modern Languages

  • C
  • C++
  • Java
  • C#
  • D, F#
  • Dynamic languages?
  • ...

Introducing C# and .NET 1 -------------------------------------------------------- Join the course or order it from semanticase.com 9. What is .NET? Introducing C# and .NET 1 -------------------------------------------------------- Join the course or order it from semanticase.com 10. What is .NET?

  • Programming framework from Microsoft
  • Large class library of ready-made types (BCL)
  • Based on a special runtime (CLR)
  • Virtualizes machine abstraction with CLR means that we do not program to a specific CPU
  • Manages memory automatically
  • Strongly typed

Introducing C# and .NET 2 -------------------------------------------------------- Join the course or order it from semanticase.com 11. What is .NET?

  • Language independent
  • Cross-language integrates
  • Defines a common type system (CTS)
  • Uses Object-Oriented techniques
  • Inter-operates with native code
  • Simple deployment (copy-paste assemblies)
  • See the list of all .NET-able languages:http://www.dotnetpowered.com/languages.aspx

Introducing C# and .NET 3 -------------------------------------------------------- Join the course or order it from semanticase.com 12. What is .NET?

                  • Non-MicrosoftCompliant framework: Mono (from Novell)
        • http://www.mono-project.com
  • A posterwith the (about) 500 commonly used types.
  • In Documents folder in the course kit:
  • DotNet 3.5 Namespaces Poster.pdf
  • It also may be available online.
  • Search online for: net namespaces poster

Introducing C# and .NET 4 -------------------------------------------------------- Join the course or order it from semanticase.com 13. CTS Common Type System

  • CTS is a formal specification for defining types in order to be hosted by the CLR.
  • .NET compilers and tools must adhere to CTS.

Introducing C# and .NET 1 -------------------------------------------------------- Join the course or order it from semanticase.com 14. .NET CTS Types

  • 'Type' is a generic name to any of the following5 CTS types (given in C# syntax):
  • class
  • struct
  • interface
  • delegate
  • enum

Introducing C# and .NET 1 -------------------------------------------------------- Join the course or order it from semanticase.com 15. Type Members

  • Code items which a CTS type can hold.
  • Here are the type members available:
  • nested type field
  • constructor read only field
  • static constructor event
  • method operator
  • indexer constant
  • property finalizer

Introducing C# and .NET 1 -------------------------------------------------------- Join the course or order it from semanticase.com 16. What is C#?

  • One of the two main languages for .NET
  • Developed by Microsoft
  • Strongly Typed (static language)
  • Object Oriented
  • Pre-compiled
  • C-Style

Introducing C# and .NET 1 -------------------------------------------------------- Join the course or order it from semanticase.com 17. C# vs. C++ Conceptual Differences

  • No global methods or variables
      • Bothmust be defined within a types.
  • No header files
      • Only source files carries compiled code.
  • .exe files are not regular executables
      • They are IL compiled from a .NET language (such as C#).

Introducing C# and .NET 1 -------------------------------------------------------- Join the course or order it from semanticase.com 18. C# vs. C++ Conceptual Differences

  • Automatic Garbage Collector:
      • Finalizers are called automatically;
      • Manual disposing for outer resources only.
  • Singular Inheritance:
      • A class can inherit from one class only
  • Classes differ from structs semantically: Struct value type,class reference type.

Introducing C# and .NET 2 -------------------------------------------------------- Join the course or order it from semanticase.com 19.

  • Free Sample: CHAPTER 6
  • Following Plato's Doctrine of Ideas
  • Objects

Practical Programming with C# 3.0 -------------------------------------------------------- Join the course or order it from semanticase.com 20. Encapsulation

  • Consider this class:
  • class User
  • {// This is bad bad bad!!!
  • public stringName =string .Empty;
  • public voidShowName()
  • {
  • Console .WriteLine(Name);
  • }
  • }

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 21. Strive to Give Less Access

  • ... because encapsulation is not like love!
  • Give only when you mustin order to supply the service!
  • Give only the level you must in order to supply the service!

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 22. Strive to Give Less Access Objects 2 -------------------------------------------------------- From: addletters.com Join the course or order it from semanticase.com 23. Access Modifiers

  • Why do access modifiers exist?
  • Which is the default for:
    • Fields:________________
    • Functions: ________________
    • Nested Types:________________
    • Independent Types:________________
  • * Independent Types CTS types (classes, structs, interfaces, enums and delegates) which are defines directly under a namespace, i.e., they are not nested inside other types.

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 24. Access Modifiers of Members

  • Who can use a memberexceptits definer?
  • public any consumer
  • protected internal inheritor OR same- assembly consumers
  • Internal same-assembly consumers only
  • protected derived consumers only
  • private no one

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 25. Access Modifiers of Types

  • Who can use an independent type?
  • public any consumer
  • Internal same-assembly consumers only
  • protected ,protected internal,private
  • cannotuse as modifiersfor an independant type.Why?

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 26. Only Private is Really Private*

  • protecteddoes not protect your type!
  • Variable fields should always be private.
  • Protected access? - Only by methods. Do you know who will be your inheritors?
  • What about protected internal ?
  • ( * By the compiler. Reflection bypasses it...)

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 27. Object Fields Auto-Initialization

  • Each field inits to the default value of its type.Do not confuse with local variables!
  • class Person
  • {
  • //Int to 0, bool to false, etc.
  • private int number;
  • //Reference types init to null
  • private Person mother;
  • }

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 28. Object Fields Manual Initialization

  • Initialize strings tostring .Empty !Why?
  • Initialize other type fields only when needed.
  • class Person
  • {
  • private Class1 c =newClass1 ();
  • private strings =string .Empty;
  • }
  • Field initializers are called before statements which are inside the constructor.

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 29. Field-Constructor Ordering

  • Fields are constructed first!
  • Task:
  • Initialize a field and check IL code. Where is the field initialized? ____________________

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 30. Read-only Object Fields

  • class MyClass
  • {
  • public readonly stringName;
  • publicMyClass( stringname)
  • {
  • this .Name = name;
  • }
  • }// Any Problems?_________

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 31. Read-only Object Fields

  • // Consuming
  • varmine =new MyClass ( "x" );
  • //mine.Name = "x"; COMPILE ERROR
  • Conclusion: Read-only object fields are safe even as public.

Objects 2 -------------------------------------------------------- Join the course or order it from semanticase.com 32. Read-only Object Fields

  • What about changingNamewithin MyClass? - Impossible!
  • // Inside MyClass
  • public voidMethod( stringname)
  • {
  • // Name = name; COMPILE ERROR
  • }

Objects 3 -------------------------------------------------------- Join the course or order it from semanticase.com 33. Supplying Parameterless Ctor

  • Compiler supplies such by default.
  • class MyClass
  • {
  • }
  • Consumer can use the default constructor:
  • varc =new MyClass ();

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 34. Supplying Parameterless Ctor

  • Be careful:
  • class MyClass
  • {
  • publicMyClass( longl)// Ctor
      • {
      • }
  • }
  • Default constructor is not supplied anymore!

Objects 2 -------------------------------------------------------- Join the course or order it from semanticase.com 35. Supplying Parameterless Ctor

  • If a parameterless ctor is required, add it:
  • class MyClass
  • {
  • publicMyClass() { }
  • publicMyClass( longl)
      • {
      • }
  • }

Objects 3 -------------------------------------------------------- Join the course or order it from semanticase.com 36. Overloading Constructors

  • Object initializer reduces the need of multiple constructors.
  • However, supplying few-parameters constructors is still helpful.
  • Example consuming a constructor which gets a string parameter for the message:
  • throw new SomeException ( "Boom!" );

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 37. Overloading Constructors

  • publicHorse( Colorcolor)
  • {
  • this .Color = color;
  • }
  • publicHorse( Colorcolor,stringname)
  • :this (color)
  • {
  • this .Name = name;
  • }

Objects 2 -------------------------------------------------------- Join the course or order it from semanticase.com 38. Overloading Constructors

  • A rich constructor should call poorer.
  • Should a no-parameter or doing-nothing ctor be called by other ctors?________ Why?______________________________

Objects 3 -------------------------------------------------------- Join the course or order it from semanticase.com 39. Manual Properties

  • Inside class Person:
  • privateHeartheart;
  • public HeartHeart
  • {
  • get{return this .heart; }
  • set{this .heart =value ; }
  • }
  • Consider assuring that heart is not null...

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 40. Manual Properties

  • Properties look like fields from the outside but act like methods on the inside.
  • You access a property as thoght it was a field:
  • Heartheart= person1.Heart;
  • person1.Name = person2.Heart;
  • Construct a property and check IL. Findings:
  • _____________________________________

Objects 2 -------------------------------------------------------- Join the course or order it from semanticase.com 41. Manual Properties

  • Not all properties need fields:
  • Storing calculate values may be considered carefully and should use only when computing is costly. Here it is not costly.
  • public boolCanWalk
  • {
  • get
  • {
  • return this .legs.Count > 0;
  • }
  • }

Objects 4 -------------------------------------------------------- Join the course or order it from semanticase.com 42. Manual Properties

  • Do not supply set-only properties!
  • // Compiles but does not comply with guidelines and FxCop rules.
  • public boolSomeValue
  • {
  • set{ }
  • }
  • Use a parameterized method instead.

Objects 5 -------------------------------------------------------- Join the course or order it from semanticase.com 43. Properties Are Not for Calculation

  • Consumer expects that properties should return immediately and not hide methods.
  • This is bad:
  • static doubleCalculatedNumber
  • {
  • get{returnCalculateNow() ; }
  • }
  • Supply a method instead.

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 44. Automatic Properties

  • Do not automatically give access!
  • public int Id { get ; set ; }
  • Think: Does logic allows changing a value?
  • This may be considered instead:
  • public int Id { get ; private set ; }

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 45. Automatic Properties

  • We cannot supply one-way access:
  • public int Id { get ; }// ERROR
  • So what can we do?
  • - Implementmanualpropertiesinstead .

Objects 2 -------------------------------------------------------- Join the course or order it from semanticase.com 46. AvoidstringAutomatic Properties

  • Strings should not be null!
  • // Bad! May be set to null
  • public string Name { get ; set ; }
  • Instead, provide a manual property backed with a field member.

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 47. Indexers

  • Allows consumer to access an object as an array.
  • varaquarium =new Aquarium (fishes);
  • //Using an indexer which gets an int
  • varfirstFish = aquarium[ 0 ];
  • Indexers cannot be static in C#.

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 48. Indexers

  • Here is the implementation of the indexer:
  • public Fish this [ intindex]
  • {
  • get{returnfishes[index]; }
  • }
  • Kind of hybrid...
  • Used like a property;
  • Get parameters as a method.

Objects 2 -------------------------------------------------------- Join the course or order it from semanticase.com 49. Indexers

  • //Using an indexer which gets a string
  • varkingFishes = aquarium[ "King" ];
  • Is it clear what does the indexer return?
  • What is the alternative to get the collection of king fishes?
  • Do not use indexers if the meaning is not clear with the quickest glance!

Objects 3 -------------------------------------------------------- Join the course or order it from semanticase.com 50. Operator Overloading (Avoid)

  • Recall:
  • Just because you can ...
    • ... doesnt mean you should!
  • Difficult to read and understand;
  • Multiple meanings possible;
  • It is not CLS-Complient;
  • Operator overloading is appropriate for implementing customstructsthat represent primitive data types. For example, a custom numeric type is an excellent candidate for operator overloading. However, most of these allready exist in .NET libraries!

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 51. Operator Overloading (Avoid)

  • Operators which one might overload:
  • Binary
  • + - * / % & | ^
  • > == != < > =
  • Unary
  • + - ! ~ ++ -- true false
  • and conversion operators

Objects 2 -------------------------------------------------------- Join the course or order it from semanticase.com 52. Operator Overloading (Avoid)

  • Example: Overloading a binary operator
  • public static MyNumber operator+(
    • MyNumberc1,MyNumberc2)
  • {
  • return new MyNumber (c1.x + c2.x);
  • }

Objects 3 -------------------------------------------------------- Join the course or order it from semanticase.com 53. Operator Overloading (Avoid)

  • Example: Overloading a unary operator
  • public static MyNumber operator-
    • MyNumberc)
  • {
  • return new MyNumber (-c.x) ;
  • }
  • The ref and out parameters are not allowed as arguments to operator overloading methods.
  • The true and false operators can be overloaded only as pairs.

Objects 4 -------------------------------------------------------- Join the course or order it from semanticase.com 54. Defining Conversions (Avoid)

  • If you don't have an extremely good reason, do not define conversions!
  • Why should an object change its very essence of being?
  • What is a clearer alternative?____________

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 55. Defining Conversions (Avoid)

  • class MyNumber
  • {
  • public intNumber {get ;set ; }
  • publicMyNumber( intnumber)
  • {
  • this .Number = number;
  • }
  • }

Objects 2 -------------------------------------------------------- Join the course or order it from semanticase.com 56. Implicit Conversions (Avoid)

  • public static
    • implicit operatorMyNumber ( intn)
  • {
  • return new MyNumber (n);
  • }
  • // Usingimplicit conversion
  • MyNumbera =3 ;
  • Implicit conversion operators mustn't throw!

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 57. Explicit Conversions (Avoid)

  • public static
    • explicit operator int ( MyNumbern)
  • {
  • returnn.Number;
  • }
  • // Usingexplicit conversion
  • intnumber = ( int )a;

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 58.

  • Declare in one file, inplement in another...
  • partial class A// In file A.cs
  • {
  • partial voidLog( intn) ;
  • }
  • partial class A// In file A.A.cs
  • {
  • partial voidLog( intn){...}
  • }

Partial Classes and Methods Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 59. Partial Methods

  • Declare when you want a method to be coded elsewhere.
  • Call the method as if it is implemented.
  • Partial Methods are implicitly private no modifiers! They must return void. No partial virtual methods.No out or ref parameters.
  • Why are all these restrictions?

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 60. Creating Enums

  • Always specify the default value to be zero!
  • Why? - Enums are actually numeric.
  • public enum SexKind
  • {
  • None,// implicitly set to 0
  • Male,// implicitly set to 1
  • Female,// Comma is allowed
  • }// Why enum at all and not bool?

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 61. Using Enums

  • SexKindsex =default ( SexKind ) ;
  • SexKindsex =SexKind .None;
  • SexKindsex =SexKind (0);// risky
      • All are the same...
  • // The logical value?____________
  • // The actual value?_____

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 62. Using Enums

  • Let's check the value of our variable:
  • if( sex==SexKind .Male)
  • {
  • // ...
  • }

Objects 2 -------------------------------------------------------- Join the course or order it from semanticase.com 63. Creating Bit Flag Enums

  • [ Flags ]
  • public enum FontStyle
  • {
  • Regular = 0x0,
  • Body = 0x1,
  • Italic = 0x2,
  • Underline = 0x4,
  • }
  • Next values of the type will be: 0x8, 0x10, 0x20, 0x40...
  • Bit flag enums without evaluation breaks bit logic. This ib becauseeach enum value which is not explicitly evaluated takes the value of the preceding enum value plus 1. In bit flag enum type we want each enumvalue to be twice as much as the preceding one.

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 64. Using Bit Flag Enums

  • varstyle =
    • FontStyle .Bold |FontStyle .Underline;
  • // Checkingif bold and only bold
  • if(style ==FontStyle .Bold){}
  • // Checkingbold, no care for the rest
  • varanyStyle = (style &FontStyle .Bold);
  • if(anyStyle ==FontStyle .Bold) {}

Objects 2 -------------------------------------------------------- Join the course or order it from semanticase.com 65. Enum Pitfalls

  • Cation: Enums are not value-safe!
  • // OMG! This actually compiles..
  • FontStylestyle = ( FontStyle ) 176 ;
  • // and run! var == 176

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 66. Enum Pitfalls

  • Here is an expensive solution:
  • booldefined =Enum .IsDefined(
    • typeof ( MyEnum ),17 );
  • booldefined =Enum .IsDefined(
    • typeof ( MyEnum ),"Bold" );
  • Find out why this technique is expensice.

Objects 2 -------------------------------------------------------- Join the course or order it from semanticase.com 67. Evaluating Objects

  • 1. Evaluating through constructor:
  • DateTimeendOfWorldDate =
    • new DateTime ( 1984 ,1 ,1 );
  • FullNamename =
    • new FullName ( "Ann-Marie" ,"David" );
  • List < Point >line =new List < Point >( 2 );

Objects 1 -------------------------------------------------------- Join the course or order it from semanticase.com 68. Evaluating Objects

  • 2. Evaluating through properties:
    • A. When constructor calling ends, using object initializer --
    • FullNamename =new FullName
    • {
  • First ="Ben" ,
    • };
    • B. Whenever, after object is built --
    • name.First ="Magen" ;

Objects 2 -------------------------------------------------------- Join the course or order it from semanticase.com 69. Evaluating Objects

  • 3. Evaluating through methods:
  • name.SetFullName( "Hava" ,"Immenu" );
  • base .SetFullName( "Hava" ,"Immenu" );
  • 4. Evaluating through private fields:
  • this .firstName ="Hava" ;
  • Of course, advised for inside use of the
  • class only!

Objects 3 -------------------------------------------------------- Join the course or order it from semanticase.com 70.

  • Appendix C
  • Visual Studio 2008
  • Exploring the environment

Practical Programming with C# 3.0-------------------------------------------------------- Join the course or order it from semanticase.com 71. Microsoft Visual Studio IDE

  • Enables Rapid DevelopmentRapid Development (RAD) with visual designers decreases extensibility watch out!
  • Integrated debugger
  • Class designerShamefully, Class Designer is not available in Visual Studio Express editions
  • Helpful Intellisense and re-factorer
  • Source control support
  • Integrates smoothly with data bases
  • Enables add-ons (like R#) IDE stands for Integrated Development Environment. There are many add-ins for Visual Studio and supporting tools. Check Visual Studio Gallery:http://www.visualstudiogallery.com/ Non-Microsoft ID Es are available, such as SharpDevelop:http://www.icsharpcode.net/OpenSource/SD/

Visual Studio 2008 1 -------------------------------------------------------- Join the course or order it from semanticase.com 72. .NET Application Types

  • Console Application
  • WPF Application
  • Windows Forms Application
  • ASP.NET Web Application
  • WPF Browser Application WPF Browser Application is similar to stand-alone WPF application but it runs on a server like a web application.
  • Recommended:
  • Some videos for beginners can be watched here:http://msdn.microsoft.com/en-us/library/bb820889.aspx

Visual Studio 2008 1 -------------------------------------------------------- Join the course or order it from semanticase.com 73. C# Compiler Your Best Friend

  • Builds 2 types of assemblies:
      • Dynamic-Linked Libraries (.dll files)
          • has no entry point
      • Executable (.exe file)
          • has a static Main() method

Visual Studio 2008 1 -------------------------------------------------------- Join the course or order it from semanticase.com 74. C# Compiler Your Best Friend

  • Can be run from the command line
  • Integrated into Visual Studio
  • Generously configurable
  • Guards you from compile time errors
  • Guides you how to fix them
  • Forget ignoring warnings!

Visual Studio 2008 2 -------------------------------------------------------- Join the course or order it from semanticase.com 75. Advised Compiler Tweaks

  • Go to Build Options:
  • Pop up context menu of your project
  • Choose Properties menu item
  • Go to Build tab
  • Treat warnings as errors.Why?
    • In Treat Warnings as Errors option choose: All

Visual Studio 2008 1 -------------------------------------------------------- Join the course or order it from semanticase.com 76. Advised Compiler Tweaks

  • Enable XML output file.What for?
    • In XML Documentation File check the check box (leave text field empty);
    • You need for treating warnings as error.
  • Suppress XML-Comments warning(advised for executable assemblies only!)
    • In Suppress Warnings text box type: 1591

Visual Studio 2008 2 -------------------------------------------------------- Join the course or order it from semanticase.com 77. Using Namespaces

  • Types defined in the same namespace?
      • Yes! Directly use them
      • No... Then declare using it:
        • usingSystem.Configuration;
  • CLR doesn't know anything about namespaces. When you access a type, CLR just needs to know the full name of the type and which assembly contains the definition of the type so that the common language runtime can load the correct assembly, find the type, and manipulate it.
  • You may use types of another namespace only if the namespace is defined in one or more of the assemblies which are referenced in your project.
  • Using can be done also by referring to the type by its full name:System.Configuration.ConfigurationSettings , but declaring with using statement is preferable for cleaner code and better readability.

Visual Studio 2008 1 -------------------------------------------------------- Join the course or order it from semanticase.com 78. Creating Aliases

  • Use when you have clashing namespaces.
      • usingSystem.Windows.Forms;
      • using BizForm= MyCompany. Form ;

Visual Studio 2008 1 -------------------------------------------------------- Join the course or order it from semanticase.com 79. Referencing Projects and Libraries

  • Add a reference (option 1)
  • Open Object Browser (CTRL + W, J)
  • Select the desired assembly or namespace
  • Click on the icon Add to References.
  • Add a reference (option 2)
  • Choose Add Reference from the context menu of your project
  • Add the desired assemblies.
  • Prefer referencing projects than output assembly files:
    • More powerful debugging capabilities
    • Easier accessing to referenced code
    • Be careful not to break other consumers' code.
    • Both .exe and .dll files may be referenced, but avoid making libraries as executable.

Visual Studio 2008 1 -------------------------------------------------------- Join the course or order it from semanticase.com 80. Refactoring

  • When does refactoring end?
      • -Only when the code is thrown away!
  • Prefer automatic refactoring.
  • Change a name of a type through its file name.
  • Consider R# add-in (pronounced: Re-sharper)
  • R# is a proprietary tool from JetBRAINS.com
  • Consider CodeRush add-in
  • CodeRush is now owned by DevExpress.com links are changing.

Visual Studio 2008 1 -------------------------------------------------------- Join the course or order it from semanticase.com 81. Refactoring

  • Mastering refactoring tools lets you...
  • Increase productivity;
  • Have more information about the plain code;
  • Work faster;
  • Avoid syntactic mistakes.
  • Tool: Clone Detective Allows you to analyze C# projects for source code that is duplicated. Having duplicates can easily lead to inconsistencies and often is an indicator for poorly factored code.http://www.codeplex.com/CloneDetectiveVS

Visual Studio 2008 2 -------------------------------------------------------- Join the course or order it from semanticase.com 82. Need Help?

  • Google and IntelliSense are the most used:
  • Diagram taken from Brad Abram's post with his findings for the survey checking how developers use VS and .NET Framework documentation. One of the questions was How do you find information? and the answers can be seen in the diagram.
  • Post: http://blogs.msdn.com/brada/archive/2008/08/11/some-results-from-visual-studio-and-net-framework-developer-documentation-survey.aspx Blog: http://blogs.msdn.com/brada/

Visual Studio 2008 1 -------------------------------------------------------- Join the course or order it from semanticase.com 83. IntelliSense Tips

  • When finding the desired item in the list...
  • If defining hit SPACE
  • If using keep on coding!

Visual Studio 2008 1 -------------------------------------------------------- Join the course or order it from semanticase.com 84. Smart Tags

  • Let automation work for you!
  • RememberCTRL + .keyboard shortcut :-)
  • Use when implementing interfaces.
  • Use when renaming.
  • Use smart tags for addingusingnamespaces (2 ways):
  • A smart tag blinks when you delete the namespace in a full name of a type within the code;
  • A smart tag blinks when you write the name of a type without IntelliSense with Pascal casing.

Visual Studio 2008 1 -------------------------------------------------------- Join the course or order it from semanticase.com 85. Snippets

  • A snippet is a small ready-made piece of code.
  • Insert a snippet :
      • Type the abbreviations and hit TAB twice
      • Or -
      • In context menu chooseI nsert Snippet...
      • Use ENTER to choose, TAB to navigate and ENTER again to go on typing.
  • Using the context menu option will ask you to choose a set of snippets when more that one set are installed.

Visual Studio 2008 1 -------------------------------------------------------- Join the course or order it from semanticase.com 86. Keyboard Shortcuts

  • Editing Intellisense
  • CTRL + . Show the smart tag CTRL + SHIFT + SPACE Show parameter info
  • CTRL + K, C Comment line CTRL + J Show completion list
  • CTRL + K, U Uncomment line CTRL +K, I Show declaration in a tool tip
  • CTRL + SHIFT + B Build the solution CTRLMake popups transparent
  • Organizing
  • CTRL + ENTER Move to higher line
  • CTRL + SHIFT + ENTER Move to lower line
  • CTRL + M, M Toggle region
  • CTRL + K, S Surround with
  • F6 is also a keyboard shortcut for building the solution. However, as it is located next to F5 which starts debugging and may cause erroneous operation.

Visual Studio 2008 1 -------------------------------------------------------- Join the course or order it from semanticase.com 87. Keyboard Shortcuts

  • Navigation Debugging
  • F8 Move to next location F9 Add or remove a breakpoint
  • F12 Move to definition F5 Start debugging
  • SHIFT + F12 List all references SHIFT + F5 End debugging
  • CTRL + ] Move to matching brace:{and} F11 Step into
  • CTRL + I Move to what I type SHIFT + F11 Step out
  • CTRL + SHIFT + 8 Move to where I was F10 Step over
  • CTRL + F/H Show Find and Replace dialog
  • CTRL + SHIFT + F/H Find or replace in many files
  • CTRL + TAB Show files navigator
  • ESC Return to code

Visual Studio 2008 2 -------------------------------------------------------- Join the course or order it from semanticase.com 88. Regions

  • Regions arevisualgrouping for easier orientation.
  • #regionPrivate Methods
  • // Some private methods come here
  • #endregion
  • Free Tool: Regionerate Automatic regioning!
  • http://www.rauchy.net/regionerate/- Omer Rauchwerger

Visual Studio 2008 1 -------------------------------------------------------- Join the course or order it from semanticase.com 89. Do not Misuse Regions

  • Regions are for grouping, not for hiding.
  • Regions do not provide encapsulation.
  • Avoid nested regions.
  • Having many regions in a type? Lots of code?
      • Extract new types or partial types!
  • Do not define many types in one file, hidden inside regions.

Visual Studio 2008 1 -------------------------------------------------------- Join the course or order it from semanticase.com 90. Debugger Has a Bad Name

  • Why?
  • - It should be used not only for debugging!
  • This is aRunner!
  • Use the debugger throughout development!
  • F5 starts debugging, SHIFT + F5 stops it.

Visual Studio 2008 1 -------------------------------------------------------- Join the course or order it from semanticase.com 91. Debug!

  • Have a bug?
  • Search for it! Debug!
  • Start thinking only after in-depth debugging.
  • Don't waste your timethinkingwhat is wrong;
  • Discoverwhat is wrong!

Visual Studio 2008 1 -------------------------------------------------------- Join the course or order it from semanticase.com 92. Debug Breakpoints

  • F9 adds or removes a breakpoint.
  • Clean up breakpoints when bug fixed.
  • Use conditional breakpoints right click on the breakpoint.
  • Do not run when code is not built this is neither VB6 not a scripting language. Well, not yet.
  • Conditional breakpoints are not available in C# Express Edition.

Visual Studio 2008 1 -------------------------------------------------------- Join the course or order it from semanticase.com 93. Debugger Windows

  • Most important windows:
  • Locals
  • Output
  • Immediate
  • Call Stack
  • Open from main menuD ebug . Windows
  • Note that some windows can be opened only after debugging has started.

Visual Studio 2008 1 -------------------------------------------------------- Join the course or order it from semanticase.com 94. Debugger Configuration

  • Catch exception when thrown:
  • Main menu .D ebug . E x ceptions...
  • CheckT hrownfor CLR exceptions

Visual Studio 2008 1 -------------------------------------------------------- Join the course or order it from semanticase.com 95. Order of Class Members

  • - Constant Fields; public, protected, private
  • - Variable Fields; always private
  • - Automatic properties; public, protected
  • - Constructors; public, protected, private
  • - Properties; public, protected
  • - Events; public, protected
  • - Methods; public, protected
  • - Explicit interface implementations
  • - Internal members
  • - Private members
  • - Nested types

Visual Studio 2008 1 -------------------------------------------------------- Join the course or order it from semanticase.com 96.

  • Manager - Order the course for your company!
  • Individual join a public course!
  • Advanced .NET courses are also available! semanticase.com

--------------------------------------------------------