Child Development Class Assignments C#

This chapter is taken from Microsoft Windows SharePoint Services Inside Out published by Microsoft Press; ISBN 0735621713; copyright Microsoft Press 2005; all rights reserved. The author, Jim Buyens, has written more than 10 books on Web-based development, including Microsoft Office FrontPage 2003 Inside Out from Microsoft Press. Jim has 20 years of experience with computer networking technologies and is a Microsoft Most Valuable Professional who contributes extensively to the Microsoft Office FrontPage online communities.

No part of these chapters may be reproduced, stored in a retrieval system, or transmitted in any form or by any means—electronic, electrostatic, mechanical, photocopying, recording, or otherwise—without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews.

Chapter 19: Beginning Web Part Development


Configuring Your Development Environment
How Web Parts Work
Starting a New Web Part Project
Writing Web Part Output
Adding Layout Controls and Content
Manipulating SharePoint Objects
Using the ASP.NET Event Model
Exposing Custom Properties
Using Web Part Tool Parts
Providing a Design Time HTML Provider
Assigning a Strong Name
Deploying Custom Web Parts
In Summary

With the flexibility of powerful prewritten Web Parts, Web Part Pages, custom lists and libraries, browser-based editing, FrontPage-based editing, Data Sources, Data Views, Web Part connections, and connectivity to Microsoft Office applications, it's amazing how much you can do with Microsoft Windows SharePoint Services without programming. Inevitably, however, no tool—or set of tools—can do everything. And with software, that's the time to think about writing your own code.

In a SharePoint environment, writing your own code usually means writing Web Parts. Web Parts integrate smoothly with the rest of your SharePoint site, leveraging its existing appearance, organization, security, and data management. What's more, Web Parts are reusable. You can write them once and use them in as many pages as you like.

Of course, these advantages come at a price. Web Parts work differently than ordinary Microsoft ASP.NET pages do, and much more differently than legacy ASP pages. As a result, developing and deploying a Web Part requires a different mind-set and a somewhat different tool set than does developing other kinds of server-based Web components. With that in mind, this chapter explains the essence of how Web Parts work and what software you need to develop them. Chapters 20 and 21 will provide step-by-step instructions for creating some simple but useful Web Parts.

The purpose of these chapters is to introduce Web Part programming. They assume you already have some experience with Microsoft .NET programming, and with Web-based programming in general. The examples are in Microsoft C, but you could use Microsoft Visual Basic .NET just as effectively.

Configuring Your Development Environment

Web Parts run only within the environment of Windows SharePoint Services, which, in turn, requires Microsoft Windows Server 2003 or later. As a result, you'll need a copy of Windows Server 2003. If possible, you should have your own server because:

  • Software development by its very nature leads to frequent Microsoft Internet Information Services (IIS) resets and occasional system reboots.
  • Interactive debugging works best on the local machine, and can monopolize resources.
  • To get your application working (or figure out why it isn't), you'll need authority to inspect or adjust any aspect of system configuration.

You can run Windows Server 2003 in any of these modes:

  • As your primary workstation operating system   This option uses the least hardware, but exposes your day-to-day work environment to changes or crashes caused by unfinished (and therefore unreliable) software. In addition, you may encounter issues running desktop software designed primarily for Microsoft Windows XP.
  • On its own computer   This option isolates your daily working environment from your development activities, but it does require another piece of hardware.
  • As a virtual machine under Microsoft Virtual PC   This option saves you the expense of buying a new computer, but it's not free. You still have to buy Microsoft Virtual PC and, in all probability, some additional memory and disk space for the PC that will run it. A major advantage, however, is the ease of saving and restoring system images. If you find that some approach or some experiment isn't working out, you can revert to an earlier system image.

To create, compile, and test the code for Web Parts, you'll need a copy of Visual Studio .NET. Any version will do, including the stand-alone products Visual C .NET Standard Edition and Visual Basic NET Standard Edition.

Microsoft Developer Network (MSDN) subscriptions come with licenses to download, install, and run Visual Studio .NET, Virtual PC, Windows Server 2003, Microsoft SQL Server, and most other Microsoft products for development use only. MSDN Universal, the top offering, includes all Microsoft Office programs and SharePoint Portal Server. For more information about this option, see Microsoft Developer Products.

How Web Parts Work

In ASP.NET terminology, a server control is a software component that runs on the Web server as part of one or more ASP.NET Web pages. This provides a way of breaking complex Web pages into manageable pieces, and of using the same component in multiple pages. There are two kinds of ASP.NET server controls: user and custom.

  • User Controls   These controls consist of two parts:
    • A file that has an .ascx file name extension, and that contains sample HTML.
    • The program code that executes on the Web server. This code, plus the code for all other ASP.NET pages and user controls in the same application, is usually compiled into a single DLL that resides in the application's bin folder. However, it can also reside in source form, within the .ascx file.

    Most developers find user controls fairly easy to create. This is because the developer (or even a Web designer) can use a visual designer to create the sample HTML, and because programming a user control is very much like programming an ordinary ASP.NET page.

    User controls, however, can be difficult to deploy widely. When a change occurs, someone must:

    1. Identify each Web site that uses the control.
    2. Install the new .ascx and source code files in each Web site.
    3. Recompile all the source code in each Web site.
    4. Copy the revised .ascx file and the recompiled code to each production Web site.

    If Web Parts were user controls, deploying a new Web Part (or upgrading one) would require recompiling and reinstalling the program code for every SharePoint site that uses the Web Part. That's why Web Parts aren't user controls.

  • Custom Controls   These controls consist entirely of program code. There's no HTML template like the .ascx file for a user control, and consequently no way of using a WYSIWYG editor to design its HTML. A programmer must write code that emits each scrap of HTML that the control needs in order to display output.

    The advantage of custom controls lies in deployment. Each custom control (or, if you choose, each logical group of custom controls) has its own DLL, and at your option you can install this once per physical server or once per virtual Web server. This is the model that Web Parts use. All Web Parts are, in fact, ASP.NET custom controls.

In practice, laying out page elements with no WYSIWYG editor and writing program code that emits HTML aren't the grueling tasks you might expect. The reason is that the output of most Web Parts is fairly simple. Many page elements—such as the top navigation bar, the page banner, any toolbars, and any link areas—are part of the Web Part Page or of other Web Parts. Any Web Parts you create need only display their specific data, and they inherit styles from the site's theme.

Windows SharePoint Services provides a base class for creating Web Parts, namely Microsoft.SharePoint.WebPartPages.WebPart. This class provides a base upon which you can write any type of Web Part you like. To inherit from this class (that is, to use it as a base), you code the following statement in the source code for your Web Part:

In addition, virtually all Web Parts override two methods in the WebPart class:

  • CreateChildControls   The WebPart class calls this method to create child server controls that the Web Part will later convert to HTML.
  • RenderWebPart   The WebPart class calls this method to render (that is, create) the HTML that the Web Part will display.

Note   If you've ever written ASP.NET custom controls that aren't Web Parts, you're probably accustomed to overriding the Render method rather than the RenderWebPart method. In a Web Part, however, the Render method creates only the chrome (the Web Part's title bar and border, for example). The RenderWebPart method creates the HTML that appears in the body of the Web Part.

The reason for overriding (that is, superseding) these methods is that the versions in the WebPart class essentially do nothing. Overriding them means that at the proper time, the WebPart class will call custom methods you provide rather than its own do-nothing methods. Adding these two overrides to the previous code gives the Web Part this structure:

For more information about overriding the CreateChildControls method, see Adding Layout Controls and Content.

For more information about overriding the RenderWebPart method, see Writing Web Part Output.

If you've written conventional ASP.NET pages or ASP.NET user controls, you probably made extensive use of the Page_Load event. A similar event occurs for custom controls, but most Web Parts never bother detecting it. There are at least two reasons for this:

  • Page_Load normally fires when ASP.NET finishes loading the page's server controls into memory. In a Web Part, however, ASP.NET doesn't load the server controls; your own CreateChildControls method does that. So, you may as well put your code in the CreateChildControls method, or call it from within that method.
  • Within a Page_Load event handler, programmers normally use the IsPostBack property to differentiate between the initial display of a page and a subsequent submission. But in the case of Web Parts, a postback also occurs when a team member uses a browser to add, remove, or reconfigure any Web Part on the page.

    This makes sense if you stop and think about it. There are many times—such as when you use a browser to add a Web Part to a page—when a round trip to the server clearly occurs. However, these aren't normal form submissions, and you shouldn't process any data that form fields might contain.

So, if you're not going to use Page_Load to initialize the display, and you're not going to use it for form submissions, you're probably not going to use it much at all.

As in conventional ASP.NET pages, most Web server controls have onServerClick or onServerChange events that can trigger event handlers in a Web Part. This is the normal way of detecting actual form submissions.

For more information about using onServerClick and onServerChange events in a Web Part, see Using the ASP.NET Event Model.

Starting a New Web Part Project

Before starting to create your first Web Part, you need to have Windows SharePoint Services up and running, and any version of Visual Studio .NET installed on the computer you plan to use for coding and compiling.

With those items in place, you're ready to install the Web Part templates For Visual Studio .NET. This is something you need to do only once. Here's the procedure:

  1. If you're not going to run Visual Studio on the SharePoint server, copy the Microsoft.SharePoint.dll file from the server to the Visual Studio machine. By default, this file resides at:

    You can copy this file to any folder you like on the Visual Studio machine; just remember its location and don't delete it by accident.

  2. On the machine where Visual Studio is installed, browse
  3. When the Downloads for SharePoint Products and Technologies page appears, click the hyperlink titled SharePoint Products And Technologies Templates: Web Part Templates For Visual Studio .NET.
  4. When the Download Details page appears, click the Download button and save the resulting file () to a temporary location.
  5. Run the file you downloaded in step 4. When the WinZip Self-Extractor dialog box appears, choose a folder for the unzipped setup files and then click Unzip.
  6. Open the folder you specified in step 5 and run the setup.exe program.
  7. Click Next on the opening page of the setup wizard, and then I Agree and Next on the License Agreement page.
  8. On the third wizard page, select the check box for each set of templates you want to install. The choices are Visual C and Visual Basic .NET. The examples in this book are all in C, but feel free to install both.
  9. If the wizard page shown in Figure 19-1 appears, enter the folder location where you saved the Microsoft.SharePoint.dll file you copied in step 1.
  10. Click Next and Finish to perform the installation.

Figure 19-1. If you install the Web Part Templates for Visual Studio .NET on a machine not running Windows SharePoint Services, you'll need to supply a local copy of Microsoft.SharePoint.dll and provide its location on this wizard page.

To actually start programming a Web Part, proceed as follows.

  1. Start Visual Studio, choose New from the File menu, and then choose Project.
  2. When the New Project dialog box shown in Figure 19-2 appears, apply the following settings and then click OK:
    • Project Types   Select either Visual Basic Projects or Visual C Projects. This presumes, of course, that you installed the corresponding templates in the previous procedure. The examples in this book are all in C.
    • Templates   Choose Web Part Library.
    • Name   Specify the name of the Visual Studio project. By default, this will also be the name of the DLL that deploys your Web Part. Visual Studio will also create a solution having this name.
    • Location   Specify a folder where the Visual Studio project and solution will reside.

    Figure 19-2. The Web Part library template initializes a project for creating Web Parts.

  3. Visual Studio will create the new project and open the class file for a Web Part named WebPart1. Figure 19-3 illustrates this result.

    Figure 19-3. This new Web Part project is ready to receive custom code.

  4. Unless you want your Web Part to be named WebPart1:
    1. Choose Find And Replace from the Edit menu, and then choose Replace.
    2. When the Replace dialog box appears, set Find What to WebPart1, Replace With to the name you want, and then click Replace All.
    3. Click the close box in the Replace dialog box, and then in the WebPart1.cs file. When Visual Studio prompts Save Changes To The Following Items?, click Yes.
    4. Right-click the WebPart1.cs file in Solution Explorer, choose Rename from the shortcut menu, and change the file name base to the name you assigned in step 4.2.

      Tip   If the Solution Explorer window isn't visible, press CTRL+ALT+L or choose Solution Explorer from the View menu.

Writing Web Part Output

For an ordinary ASP.NET page or user control, the first step after creating a new file is to design the HTML. This is also true for ASP.NET custom controls (and consequently for Web Parts) but, because there's no text file that contains "raw" HTML, you need to approach this task differently.

As you no doubt recall, every Web Part that creates output contains a method declaration like this:

ASP.NET runs this function automatically after all initialization functions, event handlers, and child functions have finished—in short, when all other processing is complete. To actually emit HTML, you have a choice of two approaches:

  • You can call the Write method of the HtmlTextWriter object named output. (Note that the RenderWebPart method receives a pointer to this object as an argument.) For example, to write a paragraph that displays the value of a variable named strMsg, you would code:

    SPEncode.HtmlEncode is a method that converts reserved HTML characters to character entity references. For example, it converts < to &lt; and > to &gt;.

    output.Write("<p>" + SPEncode.HtmlEncode(strMsg) + "</p>");
  • You can call the RenderChildren method, specifying the HtmlTextWriter object named output as a parameter. Here's an example:

To understand the RenderChildren method, you should know that every ASP.NET page, user control, and custom control has a Controls collection. This collection contains an object for each element the page or control will display. When you're working with ASP.NET pages or user controls, ASP.NET loads the Controls collection from the .aspx or .ascx file as follows:

  • Each tag you code creates a corresponding object in the Controls collection.
  • Any content that appears between tags coded appears in the Controls collection as a single System.Web.UI.LiteralControl object.

Because custom controls (and therefore Web Parts) have no .aspx or .ascx file, the Controls collection is initially empty. You can, however, write program code that adds any controls you want to the Controls collection. Here's an example:

A single call to the RenderChildren method will then tell each control, in order, to emit its HTML. If you use code such as the above to create five paragraph controls, RenderChildren would write five sets of <p> and </p> tags, each with the content you assigned.

The choice between using output.Write and RenderChildren is entirely yours. The output.Write method is easier to see and understand. Loading up the Controls collection and calling RenderChildren is more abstract, but it helps modularize your program and it guards against invalid HTML.

Although there's nothing wrong with output.Write, the examples in this book will use the RenderChildren method exclusively.

Adding Layout Controls and Content

ASP.NET provides two sets of objects you can add to a Controls collection. They are:

  • System.Web.UI.HtmlControls   These controls correspond very closely to standard HTML elements. For a complete list, see System.Web.UI.HtmlControls.

    To use these controls, the source code for your Web Part should contain this statement:

    Otherwise, you'll need to type the entire namespace every time you refer to one of its class names.

    using System.Web.UI.HtmlControls;
  • System.Web.UI.WebControls   Controls in this group are either:
    • Enhanced versions of HTML server controls. Unlike the HtmlSelect control, for example, the ListBox Web server control can trigger immediate postbacks.
    • Entirely new elements, such as a calendar, an ad rotator, and the ubiquitous DataGrid control.

    For a list of these controls, see System.Web.UI.WebControls.

    To use these controls, the source code for your Web Part should contain this statement:

    using System.Web.UI.WebControls;

The preceding section already showed some example code for creating a paragraph tag and adding text to it. Here it is again.

In fact, however, not all this code is likely to appear together in one block. The second and fourth statements must appear within the CreateChildControls method, as shown below:

The second statement, however, could easily reside in the method that determines the value of the strMsg variable. Here's an example:

In this code:

  • The statement on line 13 establishes MsgLoad as an onLoad event handler for the prgMsg object. This means that the MsgLoad method on line 3 will run as soon as the prgMsg object finishes loading.
  • The EnsureChildControls method on line 7 ensures that the CreateChildControls method has finished executing. If not, it waits for that method to complete. This avoids the embarrassing moments that occur when a method tries to manipulate an object that CreateChildControls hasn't created yet.
  • The assignment statement on line 8 stores a value into the paragraph control created on line 12.
  • The declaration of the prgMsg variable appears on line 1, which is outside both the MsgLoad method and the CreateChildControls method. This makes the prgMsg available within both of those methods.

This, by the way, plus the Web Part templates for Visual Studio .NET, is just about all the code you need to write an elementary Web Part. A Visual Studio project using this code appears in the folder of the companion CD, and a screen shot appears in Figure 19-4.

Figure 19-4. This is a very simple Web Part, and it required very little code.

Manipulating SharePoint Objects

Windows SharePoint Services provides a rich collection of objects for working with sites, lists, permissions, and other administrative objects. All these objects reside within a namespace named, logically enough, SharePoint. Its major classes include:

  • SharePoint.SPSite   Represents a SharePoint site collection. Its properties and methods provide access to the collection's subsites, templates, cross-site groups, and so forth.

    Remember: an SPSite object doesn't represent a site. Its properties and methods pertain to an entire site collection, and not to any one Web site.

  • SharePoint SPWeb   Represents an individual SharePoint site: either a top-level Web site or a subsite. It provides access to all of the site's lists, files, folders, Web Parts, and other objects.
  • SharePoint.SPList   Represents a SharePoint list. The term list, in this context, is very broad. It includes not only generic lists, but also surveys, discussion boards, document libraries, picture libraries, and form libraries.

For a complete listing and description of the objects in the SharePoint namespace, see Microsoft.SharePoint Namespace.

The statement below creates an SPWeb object named spwCurr that describes the current SharePoint Web site. In other words, it describes the site that contains the page that displayed the Web Part.

The next statement creates an SPSite object named spsColl that describes the site collection that contains the current site.

Once you have these objects, you can retrieve more information about the site or its collection by using their properties and methods. For example, after you create an SPWeb object named spwCurr as described above:

  • spwCurr.CurrentUser.LoginName   Returns the current team member's login name, such as interlacken\karen.M
  • spwCurr.CurrentUser.Name   Returns the current team member's personal name, such as Karen Archer.
  • spwCurr.Name   Gets or sets the name of the current site.
  • spwCurr.Lists   Returns a collection of SPList objects, one for each list in the site.
  • spwCurr.Delete()   Deletes the current site.

In most cases, if a property is editable, your code can simply store a value into it. Editable, in this sense, means that Windows SharePoint Services permits direct updates to a field, and that the current user has permission to perform the operation.

To enumerate a collection, code a foreach loop like the one below:

In addition, you can index into lists using either numbers or names. The following statement, for example, creates an SPList object named splNrTwo that points to the second list in the spwCurr.Lists collection:

And this statement creates an SPList object that points to a Shared Documents library:

To add objects to and remove them from SharePoint collections, you use Add and Delete methods, just as you do with most other .NET collections. The following statement, for example, creates a new Contacts list named Swamp Dwellers. The second parameter supplies the list description:

Using the ASP.NET Event Model

Web Parts experience the same lifecycle events as ordinary Web pages: Init, Load, Data-Binding, PreRender, and Unload. To capture these, add a method like the following to the class for your Web Part:

This example illustrates the code for the Load event. To capture other life cycle events, change the word Load—in two places—to Init, DataBinding, PreRender, or Unload.

To capture button clicks, drop-down menu changes, and other form events, you must create the control, add an event hander to it, and then code the event handler. The following code, for example, creates a DropDownList control, adds an event handler named Priority_Change for the SelectedIndexChanged event, and then forces the form to autosubmit whenever the team member changes the current selection.

Note the += operator in the second statement. This appends the new event handler without discarding any others for the same event. The code for the event handler itself appears below.

A Visual Studio project illustrating these techniques appears in the folder of the companion CD, and a screen shot appears in Figure 19-5.

Figure 19-5. This Web Part traces and displays its lifecycle steps. The current display reflects four submissions.

This figure illustrates four executions of the Web Part Page, and therefore of the Web Part.

  • The Load and PreRender event handlers display simple messages, and fire for each execution.
  • The following code creates the drop-down list:

    And this code populates the list items:

    The message Priority List Loaded appears only once in Figure 19-5 because the ASP.NET ViewState mechanism persists the items (and the current selection) from one execution to another.

    The messages Priority Is Now 2 and Priority Is Now 3 appear because the team member changed the selection in the drop-down list box, and this caused an autopostback.

    lstPriority = new DropDownList(); lstPriority.SelectedIndexChanged += new EventHandler(Priority_Change); lstPriority.AutoPostBack = true;
    (lstPriority.Items.Count < 1) { lstPriority.Items.Add(new ListItem("High","1")); lstPriority.Items.Add(new ListItem("Medium","2")); lstPriority.Items.Add(new ListItem("Low","3")); litMsgs.Text += "<br>" + DateTime.Now.ToLongTimeString() + " Priority List Loaded."; }
  • The message Submit Button Clicked appears because the team member clicked the Submit button. The following code creates this button and specifies a Click event handler named Submit_Click.

    Here's the code for the event handler:

    btnSubmit = new Button(); btnSubmit.Text = "Submit"; btnSubmit.Click += new EventHandler (Submit_Click);
    Private void Submit_Click(object sender, EventArgs e) { EnsureChildControls(); litMsgs.Text += "<br>" + DateTime.Now.ToLongTimeString() + " Submit Button Clicked."; }

As in all ASP.NET pages, a single HTML form surrounds the content of each Web Part Page. Therefore, you needn't ever (and shouldn't ever) add your own <form> tags or HtmlForm objects to a Web Part. The Web Part Page will provide these.

When a Web Part Page displays several Web Parts, they all execute every time the Web page makes a round trip to the server. For example, your Web Part will execute:

  • Every time the team member clicks the browser's Refresh button.
  • Every time the team member clicks a Submit button or otherwise creates a postback from another Web Part.
  • Every time the team members use browser-based design features such as:
    • Switching from shared view to personal view.
    • Entering, using, or exiting browser-based Design mode.
    • Displaying the Add Web Parts task pane.
    • Scrolling through multiple pages in a Web Part gallery.
    • Modifying a Web Part.

As a result, any event handlers in your Web Part that book orders, adjust account balances, or perform any other kinds of updates should only react to events that come from elements that you know reside in your Web Part, and not to general, page-wide events like Page_Load.

Exposing Custom Properties

Many Web Parts expose properties that team members can change through the Modify Web Part task pane in the browser, or through the Web Part Properties dialog box in Microsoft Office FrontPage. The Text property in Figure 19-6 provides an example. Windows SharePoint Services calls these custom properties.

Figure 19-6. The input field titled Text is a SharePoint custom property. Windows SharePoint Services does most of the work in displaying and saving such values.

The code that appears below creates a custom property named Text. You put this inside the class definition for your Web Part, but not inside any method. The Web Part templates for Visual Studio .NET provide a version of this code in every new Web Part class file.

The first two lines define a default value and a class-wide variable for the custom property value. The last five lines are a very ordinary property definition. The middle six lines, within square brackets, actually define the custom property as it will appear in the Web Part's property pane. Table 19-1 lists and describes all the available properties.

Table 19-1. Custom property attributes

BrowsableA value of false stops the custom property from appearing in the Web Part's property sheet The default is true. Setting the WebPartStorage attribute to Storage.None has the same effect.
CategoryThe section title where the custom property will appear on the property sheet. If you leave this attribute empty or set it to Default, Appearance, Layout, or Advanced, the custom property will appear in the Miscellaneous section.
DescriptionThe tool tip text that appears if a team member hovers the mouse pointer over the custom property's input control.
DefaultValueThe custom property's default value.
FriendlyNameAttributeThe caption or title that identifies the custom property. If you leave this empty, the program name for the property will appear.
ReadOnlyA value of true makes the custom property read-only in the property sheet. The default is false.
WebPartStorageThe view modes for which Windows SharePoint Services will display and save custom property values. The permissible values are:
  • Storage.Shared   Only when configuring the Web Part's shared view.
  • Storage.Personal   When configuring either the shared or personal view.
  • Storage.None   Never.
HtmlDesignerAttributeAssociates a property builder (that is, a custom Web page or module) with the property. This overrides the normal input format in Web page designers such as FrontPage.

You can define as many of these custom properties as you need. Windows SharePoint Services takes care of displaying them, and of saving the configured values as part of the Web Part definition for that page (or, in the case of personal views, for that team member). The type of input control depends on the data type of the custom property. For example, a string property will get a text box, and a Boolean property will get a check box.

Of course, it's your responsibility to write code that makes use of the custom property, once received.

If you need the capability of rejecting a custom property, choose one of these approaches:

  • Raise an exception in the property's set method. This approach is simple and normally works as you want in both the SharePoint browser interface and in FrontPage. Instead of closing, the property sheet will display the error message from your exception.

    If, however, your exception occurs when the Web Part's property sheet isn't visible, then the whole page may fail. This can occur, for example, when another control tries to set the property pragmatically.

  • Use a Web Part Tool Part, as the next section describes.

Using Web Part Tool Parts

If the default user interface that Windows SharePoint Services provides for custom properties doesn't meet your needs, Web Part Tool Parts may provide an answer.

Basically, a Web Part Tool Part is a class that displays and processes an HTML user interface for one or more custom properties. To use Web Part Tool Parts, the Web Part class file must override the GetToolParts method with code like this:

Notice that this override returns an array of ToolPart objects.

  • The first is a standard CustomPropertyToolPart object, which displays any custom properties just as the previous section described. If you override GetToolParts and omit this entry, no controls for custom properties will appear.
  • The second is a WebPartToolPart object, which displays the standard Appearance, Layouts, and Advanced sections of the Web Part's property pane. If you omit this entry, these sections won't appear.
  • The third (and each subsequent) is a custom class that you develop separately in the same project, and that inherits the Microsoft.SharePoint.WebPartPages.ToolPart class.

Any ToolPart classes you write must override four methods: ApplyChanges, SyncChanges, CancelChanges, and RenderToolPart.

For more information about writing classes that override the ToolPart class, and about using Web Part tool tips in general, see Creating a Web Part with a Custom Tool Part.

Providing a Design Time HTML Provider

If you open a Web Part Page in FrontPage, any custom Web Parts you develop are likely to appear with the following message in place of a WYSIWYG display:

The preview for this Web Part is not available.

This is because FrontPage Design view doesn't actually run any Web Parts it displays: instead, it calls a method called GetDesignTimeHtml and displays the resulting HTML. If your Web Part doesn't override this method, the message shown above appears.

The GetDesignTimeHtml method is part of an interface named IDesignTimeHtmlProvider. Here, then, are the steps necessary for your Web Part to appear graphically in an editor like FrontPage.

  • To signify that your Web Part class supports the IDesignTimeHtmlProvider interface, append a comma and the interface name to the class declaration. For example, change:


    public class SimpleWebPart : Microsoft.SharePoint.WebPartPages.WebPart {}
    public class SimpleWebPart : Microsoft.SharePoint.WebPartPages.WebPart, IDesignTimeHtmlProvider {}
  • Add a public method such as the following to your class:
    public string GetDesignTimeHtml() { return "<p>Any HTML</p>"; }

Of course, the GetDesignTimeHtml method can return any HTML you want. You might arrange, for example, for a common method to format actual data in the RenderWebPart method, and sample data in the GetDesignTimeHtml method.

For more information about using the IDesignTimeHtmlProvider interface, see "Implement the IDesignTimeHtmlProvider Interface in Your Web Part to Ensure Correct Rendering in FrontPage" in Best Practices for Developing Web Parts for SharePoint Products and Technologies.

Assigning a Strong Name

Strong naming is a technique whereby Visual Studio uses a private key to digitally sign any assemblies (for example, any DLLs) it creates. It also stamps those assemblies with a public key that can validate the signature. This guards against any unauthorized versions of a program (such as a Web Part). If the given public key fails to validate the digital signature, or if the assembly contains an unexpected public key, Windows SharePoint Services will refuse to run the module. Instead, you get a message like the following:

A Web Part or Web Form Control on this Web Part Page cannot be displayed or imported because it is not registered on this site as safe.

As you'll learn in the next section, there are two ways of deploying Web Parts: one that requires a strong name and one that recommends one. Here is the procedure for giving a Web Part assembly a strong name:

  1. Identify the folder that contains your Visual Studio Web Part project. The following is a typical path, where <user>is your windows logon ID and <project> is the name of your Visual Studio project.
    C:\Documents and Settings\<user>\My Documents\Visual Studio Projects\<project>
  2. On the machine running Visual Studio, open a command prompt.
  3. Change to the directory containing the sn.exe file. This is usually one of the following:
    C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Bin\ C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin\
  4. Create a key pair file by running the following command:

    where <path> is the path to your Visual Studio project (from step 1) and <project> is the name of your Visual Studio project.

    sn.exe –k "<path>\<project>.snk"
  5. Open your Visual Studio project, and then choose Add Existing Item from the File menu.
  6. When the Add Existing Item dialog box appears, find and select the file you created in step 4, and then click Open.
  7. In Video Studio, open the AssemblyInfo.cs file for your project.
  8. Scroll to the bottom of the file, and change the line


    where <project> is the name of your Visual Studio project.

    As usual in C literals, a single backslash is an escape character. For example, \n means new line, not a backslash followed by an n. To get a backslash in the literal's value, you have to code two backslashes.

    The path ..\..\ is necessary because the strong name key file resides two directory levels closer to the root than the DLL file Visual Studio creates when it builds your project.

    [assembly: AssemblyKeyFile("")]
    [assembly: AssemblyKeyFile("..\\..\\<project>.snk")]
  9. Scroll about halfway up the AssemblyInfo.cs file and look for a line like:

    If this line contains any asterisks, replace them with digits. This will avoid creating a new version number every time you compile the Web Part.

    [assembly: AssemblyVersion("")]
  10. Choose Rebuild Solution from the Build menu. This (and every subsequent build) will create a strongly named assembly.

Tip   Whenever the name, version number, or digital signature of a Web Part changes, you have to reinstall the Web Part on each SharePoint server that uses it, and reconfigure each Web Part page that uses it. as a result, most programmers avoid changing these properties.

Deploying Custom Web Parts

To deploy or upgrade a Web Part, you must first compile it in Visual Studio. Then, you must copy the resulting assembly (that is, the resulting .dll file) into one of two locations:

  • The SharePoint server's Global Assembly Cache (GAC)   This makes the Web Part available to all virtual servers on the same computer, with maximum privileges. However, the assembly must have a strong name. (You must have compiled it with a key pair file.)

    To install a Web Part this way, drag its .dll file into the folder, where is the server's system folder.

  • The bin folder of one or more virtual servers   This makes the Web Part available only on those virtual servers where you install it. Assemblies you install this way don't need strong names, but strongly naming your assemblies is always a good idea.

    Unfortunately, at least through windows SharePoint Services Service Pack 1, this method doesn't work properly on computers running more than one SharePoint content server.

There are three ways to install Web Parts into the bin folder of a virtual server.

  • Install the Web Part manually.   To do this:
    1. Create your own .dwp file.
    2. Copy this .dwp file into the wpcatalog folder at the virtual server's physical root.
class WebPart1 : Microsoft.SharePoint.WebPartPages.WebPart { // Your custom code goes here. }
class WebPart1 : Microsoft.SharePoint.WebPartPages.WebPart { // Global declarations go here. protected override void CreateChildControls () { // Code to create a list of Web or HTML server // controls goes here } protected override void RenderWebPart(HtmlTextWriter output) { // Code to render the list of Web or HTML server // controls goes here } }
protected override void RenderWebPart(HtmlTextWriter output) { }
HtmlGenericControl prgMsg; // Declare variable. prgMsg = new HtmlGenericControl("p"); // Create object. prgMsg.InnerText = strMsg; // Set value inside <p> // and </p>. Controls.Add(prgMsg); // Add to Controls // collection.
HtmlGenericControl prgMsg; prgMsg = new HtmlGenericControl("p"); prgMsg.InnerText = strMsg; Controls.Add(prgMsg);
protected override void CreateChildControls () { prgMsg = new HtmlGenericControl("p"); Controls.Add(prgMsg); }
HtmlGenericControl prgMsg; public void MsgLoad(object sender, EventArgs e) { string strMsg; strMsg = "Help! I'm a prisoner in a Web Part!"; EnsureChildControls(); prgMsg.InnerText = strMsg; } protected override void CreateChildControls () { prgMsg = new HtmlGenericControl("p"); prgMsg.Load += new EventHandler(MsgLoad); Controls.Add(prgMsg); }
SPWeb spwCurr = SPControl.GetContextWeb(Context);
SPSite spsColl = SPControl.GetContextSite(Context);
(SPList splCurr in spwCurr.Lists) { // splCurr will point to a different list during each iteration }
SPList splNrTwo = spwCurr.Lists[1];
SPList splSharedDocs = spwCurr.Lists["Shared Documents"];
spwCurr.Lists.Add("Swamp Dwellers", "Ducks, Frogs, Gators, and other creepy critters", SPListTemplateType.Contacts);
Protected override void OnLoad(EventArgs e) { // Your code goes here base.OnLoad(e); }
lstPriority = new DropDownList(); lstPriority.SelectedIndexChanged += new EventHandler(Priority_Change); lstPriority.AutoPostBack = true;
private void Priority_Change(object sender, EventArgs e) { // Your code goes here. }
private const string defaultText = ""; private string text = defaultText; [Browsable(true), Category("Miscellaneous"), DefaultValue(defaultText), WebPartStorage(Storage.Personal), FriendlyName("Text"), Description("Text Property")] public string Text { get { return text; } set { text = value; } }
public override ToolPart[] GetToolParts() { ToolPart[] toolparts = new ToolPart[3]; toolparts[0] = new CustomPropertyToolPart(); toolparts[1] = new WebPartToolPart(); toolparts[2] = new WebPartLibrary2.ToolPart1(); return toolparts; }

About C Sharp

C# is one of several languages that target the Microsoft Common Language Runtime (CLR). Languages that target the CLR benefit from features such as cross-language integration and exception handling, enhanced security, a simplified model for component interaction, and debugging and profiling services. Of today’s CLR languages, C# is the most widely used for complex, professional development projects that target the Windows desktop, mobile, or server environments.

C# is an object oriented, strongly-typed language. The strict type checking in C#, both at compile and run times, results in the majority of typical C# programming errors being reported as early as possible, and their locations pinpointed quite accurately. This can save a lot of time in C Sharp programming, compared to tracking down the cause of puzzling errors which can occur long after the offending operation takes place in languages which are more liberal with their enforcement of type safety. However, a lot of C# coders unwittingly (or carelessly) throw away the benefits of this detection, which leads to some of the issues discussed in this C# tutorial.

About This C Sharp Programming Tutorial

This tutorial describes 10 of the most common C# programming mistakes made, or problems to be avoided, by C# programmers and provide them with help.

While most of the mistakes discussed in this article are C# specific, some are also relevant to other languages that target the CLR or make use of the Framework Class Library (FCL).

Common C# Programming Mistake #1: Using a reference like a value or vice versa

Programmers of C++, and many other languages, are accustomed to being in control of whether the values they assign to variables are simply values or are references to existing objects. In C Sharp programming, however, that decision is made by the programmer who wrote the object, not by the programmer who instantiates the object and assigns it to a variable. This is a common “gotcha” for those trying to learn C# programming.

If you don’t know whether the object you’re using is a value type or reference type, you could run into some surprises. For example:

As you can see, both the and objects were created the exact same way, but the value of remained unchanged when a new coordinate value was assigned to , whereas the value of was modified when a new color was assigned to . We can therefore deduce that and each contain their own copy of a object, whereas and contain references to the same object. But how can we know that without doing this experiment?

The answer is to look at the definitions of the object types (which you can easily do in Visual Studio by placing your cursor over the name of the object type and pressing F12):

As shown above, in C# programming, the keyword is used to define a value type, while the keyword is used to define a reference type. For those with a C++ background, who were lulled into a false sense of security by the many similarities between C++ and C# keywords, this behavior likely comes as a surprise that may have you asking for help from a C# tutorial.

If you’re going to depend on some behavior which differs between value and reference types – such as the ability to pass an object as a method parameter and have that method change the state of the object – make sure that you’re dealing with the correct type of object to avoid C# programming problems.

Common C# Programming Mistake #2: Misunderstanding default values for uninitialized variables

In C#, value types can’t be null. By definition, value types have a value, and even uninitialized variables of value types must have a value. This is called the default value for that type. This leads to the following, usually unexpected result when checking if a variable is uninitialized:

Why isn’t null? The answer is that is a value type, and the default value for a is (0,0), not null. Failure to recognize this is a very easy (and common) mistake to make in C#.

Many (but not all) value types have an property which you can check to see if it is equal to its default value:

When you’re checking to see if a variable has been initialized or not, make sure you know what value an uninitialized variable of that type will have by default and don’t rely on it being null..

Common C# Programming Mistake #3: Using improper or unspecified string comparison methods

There are many different ways to compare strings in C#.

Although many programmers use the operator for string comparison, it is actually one of the least desirable methods to employ, primarily because it doesn’t specify explicitly in the code which type of comparison is wanted.

Rather, the preferred way to test for string equality in C# programming is with the method:

The first method signature (i.e., without the parameter), is actually the same as using the operator, but has the benefit of being explicitly applied to strings. It performs an ordinal comparison of the strings, which is basically a byte-by-byte comparison. In many cases this is exactly the type of comparison you want, especially when comparing strings whose values are set programmatically, such as file names, environment variables, attributes, etc. In these cases, as long as an ordinal comparison is indeed the correct type of comparison for that situation, the only downside to using the method without a is that somebody reading the code may not know what type of comparison you’re making.

Using the method signature that includes a every time you compare strings, though, will not only make your code clearer, it will make you explicitly think about which type of comparison you need to make. This is a worthwhile thing to do, because even if English may not provide a whole lot of differences between ordinal and culture-sensitive comparisons, other languages provide plenty, and ignoring the possibility of other languages is opening yourself up to a lot of potential for errors down the road. For example:

The safest practice is to always provide a parameter to the method. Here are some basic guidelines:

  • When comparing strings that were input by the user, or are to be displayed to the user, use a culture-sensitive comparison ( or ).
  • When comparing programmatic strings, use ordinal comparison ( or ).
  • and are generally not to be used except in very limited circumstances, because ordinal comparisons are more efficient. If a culture-aware comparison is necessary, it should usually be performed against the current culture or another specific culture.

In addition to the method, strings also provide the method, which gives you information about the relative order of strings instead of just a test for equality. This method is preferable to the , , and operators, for the same reasons as discussed above–to avoid C# problems.

Related: 12 Essential .NET Interview Questions

Common C# Programming Mistake #4: Using iterative (instead of declarative) statements to manipulate collections

In C# 3.0, the addition of Language-Integrated Query (LINQ) to the language changed forever the way collections are queried and manipulated. Since then, if you’re using iterative statements to manipulate collections, you didn’t use LINQ when you probably should have.

Some C# programmers don’t even know of LINQ’s existence, but fortunately that number is becoming increasingly small. Many still think, though, that because of the similarity between LINQ keywords and SQL statements, its only use is in code that queries databases.

While database querying is a very prevalent use of LINQ statements, they actually work over any enumerable collection (i.e., any object that implements the IEnumerable interface). So for example, if you had an array of Accounts, instead of writing a C# List foreach:

you could just write:

While this is a pretty simple example of how to avoid this common C# programming problem, there are cases where a single LINQ statement can easily replace dozens of statements in an iterative loop (or nested loops) in your code. And less code general means less opportunities for bugs to be introduced. Keep in mind, however, there may be a trade-off in terms of performance. In performance-critical scenarios, especially where your iterative code is able to make assumptions about your collection that LINQ cannot, be sure to do a performance comparison between the two methods.

LINQ is great for abstracting the task of manipulating collections, whether they are in-memory objects, database tables, or XML documents. In a perfect world, you wouldn’t need to know what the underlying objects are. But the error here is assuming we live in a perfect world. In fact, identical LINQ statements can return different results when executed on the exact same data, if that data happens to be in a different format.

For instance, consider the following statement:

What happens if one of the object’s equals “Active” (note the capital A)? Well, if was a object (that was set up with the default case-insensitive configuration), the expression would still match that element. However, if was in an in-memory array, it would not match, and would therefore yield a different result for total.

But wait a minute. When we talked about string comparison earlier, we saw that the operator performed an ordinal comparison of strings. So why in this case is the operator performing a case-insensitive comparison?

The answer is that when the underlying objects in a LINQ statement are references to SQL table data (as is the case with the Entity Framework DbSet object in this example), the statement is converted into a T-SQL statement. Operators then follow T-SQL programming rules, not C# programming rules, so the comparison in the above case ends up being case insensitive.

In general, even though LINQ is a helpful and consistent way to query collections of objects, in reality you still need to know whether or not your statement will be translated to something other than C# under the hood to ensure that the behavior of your code will be as expected at runtime.

Common C# Programming Mistake #6: Getting confused or faked out by extension methods

As mentioned earlier, LINQ statements work on any object that implements IEnumerable. For example, the following simple function will add up the balances on any collection of accounts:

In the above code, the type of the myAccounts parameter is declared as . Since references a method (C# uses the familiar “dot notation” to reference a method on a class or interface), we’d expect to see a method called on the definition of the interface. However, the definition of , makes no reference to any method and simply looks like this:

So where is the method defined? C# is strongly typed, so if the reference to the method was invalid, the C# compiler would certainly flag it as an error. We therefore know that it must exist, but where? Moreover, where are the definitions of all the other methods that LINQ provides for querying or aggregating these collections?

The answer is that is not a method defined on the interface. Rather, it is a static method (called an “extension method”) that is defined on the class:

So what makes an extension method different from any other static method and what enables us to access it in other classes?

The distinguishing characteristic of an extension method is the modifier on its first parameter. This is the “magic” that identifies it to the compiler as an extension method. The type of the parameter it modifies (in this case ) denotes the class or interface which will then appear to implement this method.

(As a side point, there’s nothing magical about the similarity between the name of the interface and the name of the class on which the extension method is defined. This similarity is just an arbitrary stylistic choice.)

With this understanding, we can also see that the function we introduced above could instead have been implemented as follows:

The fact that we could have implemented it this way instead raises the question of why have extension methods at all? Extension methods are essentially a convenience of the C# programming language that enables you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.

Extension methods are brought into scope by including a statement at the top of the file. You need to know which C# namespace includes the extension methods you’re looking for, but that’s pretty easy to determine once you know what it is you’re searching for.

When the C# compiler encounters a method call on an instance of an object, and doesn’t find that method defined on the referenced object class, it then looks at all extension methods that are within scope to try to find one which matches the required method signature and class. If it finds one, it will pass the instance reference as the first argument to that extension method, then the rest of the arguments, if any, will be passed as subsequent arguments to the extension method. (If the C# compiler doesn’t find any corresponding extension method within scope, it will throw an error.)

Extension methods are an example of “syntactic sugar” on the part of the C# compiler, which allows us to write code that is (usually) clearer and more maintainable. Clearer, that is, if you’re aware of their usage. Otherwise, it can be a bit confusing, especially at first.

While there certainly are advantages to using extension methods, they can cause problems and a cry for C# programming help for those developers who aren’t aware of them or don’t properly understand them. This is especially true when looking at code samples online, or at any other pre-written code. When such code produces compiler errors (because it invokes methods that clearly aren’t defined on the classes they’re invoked on), the tendency is to think the code applies to a different version of the library, or to a different library altogether. A lot of time can be spent searching for a new version, or phantom “missing library”, that doesn’t exist.

Even developers who are familiar with extension methods still get caught occasionally, when there is a method with the same name on the object, but its method signature differs in a subtle way from that of the extension method. A lot of time can be wasted looking for a typo or error that just isn’t there.

Use of extension methods in C# libraries is becoming increasingly prevalent. In addition to LINQ, the Unity Application Block and the Web API framework are examples of two heavily-used modern libraries by Microsoft which make use of extension methods as well, and there are many others. The more modern the framework, the more likely it is that it will incorporate extension methods.

Of course, you can write your own extension methods as well. Realize, however, that while extension methods appear to get invoked just like regular instance methods, this is really just an illusion. In particular, your extension methods can’t reference private or protected members of the class they’re extending and therefore cannot serve as a complete replacement for more traditional class inheritance.

Common C# Programming Mistake #7: Using the wrong type of collection for the task at hand

C# provides a large variety of collection objects, with the following being only a partial list:
, , , , , , , , , , , , , , .

While there can be cases where too many choices is as bad as not enough choices, that isn’t the case with collection objects. The number of options available can definitely work to your advantage. Take a little extra time upfront to research and choose the optimal collection type for your purpose. It will likely result in better performance and less room for error.

If there’s a collection type specifically targeted at the type of element you have (such as string or bit) lean toward using that one first. The implementation is generally more efficient when it’s targeted to a specific type of element.

To take advantage of the type safety of C#, you should usually prefer a generic interface over a non-generic one. The elements of a generic interface are of the type you specify when you declare your object, whereas the elements of non-generic interfaces are of type object. When using a non-generic interface, the C# compiler can’t type-check your code. Also, when dealing with collections of primitive value types, using a non-generic collection will result in repeated boxing/unboxing of those types, which can result in a significant negative performance impact when compared to a generic collection of the appropriate type.

Another common C# problem is to write your own collection object. That isn’t to say it’s never appropriate, but with as comprehensive a selection as the one .NET offers, you can probably save a lot of time by using or extending one that already exists, rather than reinventing the wheel. In particular, the C5 Generic Collection Library for C# and CLI offers a wide array of additional collections “out of the box”, such as persistent tree data structures, heap based priority queues, hash indexed array lists, linked lists, and much more.

Common C# Programming Mistake #8: Neglecting to free resources

The CLR environment employs a garbage collector, so you don’t need to explicitly free the memory created for any object. In fact, you can’t. There’s no equivalent of the C++ operator or the function in C . But that doesn’t mean that you can just forget about all objects after you’re done using them. Many types of objects encapsulate some other type of system resource (e.g., a disk file, database connection, network socket, etc.). Leaving these resources open can quickly deplete the total number of system resources, degrading performance and ultimately leading to program faults.

While a destructor method can be defined on any C# class, the problem with destructors (also called finalizers in C#) is that you can’t know for sure when they will be called. They are called by the garbage collector (on a separate thread, which can cause additional complications) at an indeterminate time in the future. Trying to get around these limitations by forcing garbage collection with is not a C# best practice, as that will block the thread for an unknown amount of time while it collects all objects eligible for collection.

This is not to say there are no good uses for finalizers, but freeing resources in a deterministic way isn’t one of them. Rather, when you’re operating on a file, network or database connection, you want to explicitly free the underlying resource as soon as you are done with it.

Resource leaks are a concern in almost any environment. However, C# provides a mechanism that is robust and simple to use which, if utilized, can make leaks a much rarer occurrence. The .NET framework defines the interface, which consists solely of the method. Any object which implements expects to have that method called whenever the consumer of the object is finished manipulating it. This results in explicit, deterministic freeing of resources.

If you are creating and disposing of an object within the context of a single code block, it is basically inexcusable to forget to call , because C# provides a statement that will ensure gets called no matter how the code block is exited (whether it be an exception, a return statement, or simply the closing of the block). And yes, that’s the same statement mentioned previously that is used to include C# namespaces at the top of your file. It has a second, completely unrelated purpose, which many C# developers are unaware of; namely, to ensure that gets called on an object when the code block is exited:

By creating a block in the above example, you know for sure that will be called as soon as you’re done with the file, whether or not throws an exception.

Common C# Programming Mistake #9: Shying away from exceptions

C# continues its enforcement of type safety into runtime. This allows you to pinpoint many types of errors in C# much more quickly than in languages such as C++, where faulty type conversions can result in arbitrary values being assigned to an object’s fields. However, once again, programmers can squander this great feature, leading to C# problems. They fall into this trap because C# provides two different ways of doing things, one which can throw an exception, and one which won’t. Some will shy away from the exception route, figuring that not having to write a try/catch block saves them some coding.

For example, here are two different ways to perform an explicit type cast in C#:

The most obvious error that could occur with the use of Method 2 would be a failure to check the return value. That would likely result in an eventual NullReferenceException, which could possibly surface at a much later time, making it much harder to track down the source of the problem. In contrast, Method 1 would have immediately thrown an making the source of the problem much more immediately obvious.

Moreover, even if you remember to check the return value in Method 2, what are you going to do if you find it to be null? Is the method you’re writing an appropriate place to report an error? Is there something else you can try if that cast fails? If not, then throwing an exception is the correct thing to do, so you might as well let it happen as close to the source of the problem as possible.

Here are a couple of examples of other common pairs of methods where one throws an exception and the other does not:

Some C# developers are so “exception adverse” that they automatically assume the method that doesn’t throw an exception is superior. While there are certain select cases where this may be true, it is not at all correct as a generalization.

As a specific example, in a case where you have an alternative legitimate (e.g., default) action to take if an exception would have been generated, then that the non-exception approach could be a legitimate choice. In such a case, it may indeed be better to write something like this:

instead of:

However, it is incorrect to assume that is therefore necessarily the “better” method. Sometimes that’s the case, sometimes it’s not. That’s why there are two ways of doing it. Use the correct one for the context you are in, remembering that exceptions can certainly be your friend as a developer.

Common C# Programming Mistake #10: Allowing compiler warnings to accumulate

While this problem is definitely not C# specific, it is particularly egregious in C# programming since it abandons the benefits of the strict type checking offered by the C# compiler.

Warnings are generated for a reason. While all C# compiler errors signify a defect in your code, many warnings do as well. What differentiates the two is that, in the case of a warning, the compiler has no problem emitting the instructions your code represents. Even so, it finds your code a little bit fishy, and there is a reasonable likelihood that your code doesn’t accurately reflect your intent.

A common simple example for the sake of this C# programming tutorial is when you modify your algorithm to eliminate the use of a variable you were using, but you forget to remove the variable declaration. The program will run perfectly, but the compiler will flag the useless variable declaration. The fact that the program runs perfectly causes programmers to neglect to fix the cause of the warning. Furthermore, coders take advantage of a Visual Studio feature which makes it easy for them to hide the warnings in the “Error List” window so they can focus only on the errors. It doesn’t take long until there are dozens of warnings, all of them blissfully ignored (or even worse, hidden).

But if you ignore this type of warning, sooner or later, something like this may very well find its way into your code:

And at the speed Intellisense allows us to write code, this error isn’t as improbable as it looks.

You now have a serious error in your program (although the compiler has only flagged it as a warning, for the reasons already explained), and depending on how complex your program is, you could waste a lot of time tracking this one down. Had you paid attention to this warning in the first place, you would have avoided this problem with a simple five-second fix.

Remember, the C Sharp compiler gives you a lot of useful information about the robustness of your code… if you’re listening. Don’t ignore warnings. They usually only take a few seconds to fix, and fixing new ones when they happen can save you hours. Train yourself to expect the Visual Studio “Error List” window to display “0 Errors, 0 Warnings”, so that any warnings at all make you uncomfortable enough to address them immediately.

Of course, there are exceptions to every rule. Accordingly, there may be times when your code will look a bit fishy to the compiler, even though it is exactly how you intended it to be. In those very rare cases, use around only the code that triggers the warning, and only for the warning ID that it triggers. This will suppress that warning, and that warning only, so that you can still stay alert for new ones.


C# is a powerful and flexible language with many mechanisms and paradigms that can greatly improve productivity. As with any software tool or language, though, having a limited understanding or appreciation of its capabilities can sometimes be more of an impediment than a benefit, leaving one in the proverbial state of “knowing enough to be dangerous”.

Using a C Sharp tutorial like this one to familiarize oneself with the key nuances of C#, such as (but by no means limited to) the problems raised in this article, will help in C# optimization while avoiding some of its more common pitfalls of the language.

Related: 6 Essential C# Interview Questions

Understanding the Basics

What is C#?

C# is one of several programming languages that target the Microsoft CLR, which automatically gives it the benefits of cross-language integration and exception handling, enhanced security, a simplified model for component interaction, and debugging and profiling services.

What is the difference between C++ and C#?

C++ and C# are two completely different languages, despite similarities in name and syntax. C# was designed to be somewhat higher-level than C++, and the two languages also take different approaches to details like who determines whether a parameter is passed by reference or by value.

Why is C# used?

C# is used for many reasons, but the benefits of the Microsoft CLR tool set and a large developer community are two main draws to the language.

About the author

Patrick Ryder, United States

member since October 9, 2012

T-SQLC++C#Microsoft SQL ServerWindows ServerWindowsMicrosoft Visual StudioASP.NET MVCASP.NET.NET+   more

Pat has over a dozen years of experience developing applications on the .NET platform, including having been part of the development team at Microsoft which created the platform itself. He also develops for Windows native API (Win32/COM), he communicates extremely well, and he has worked in teams of all sizes. [click to continue...]

Hiring? Meet the Top 10 Freelance C# Developers for Hire in March 2018

0 thoughts on “Child Development Class Assignments C#

Leave a Reply

Your email address will not be published. Required fields are marked *