Welcome to the Official Visual Studio 2008 C# Samples
We have created a set of language samples to help you get familiar with various C# language features.
- Yield Sample:This sample shows how to create a list class which implements IEnumerable<int> and the yield keyword to enable foreach iteration over the contents
of the list. Two properties are defined, one returning the odd numbers, the other returning the even numbers.
- XML Documentation Sample: This sample shows how to use XML to document code.
- Versioning Sample: This sample demonstrates versioning in C# through the use of the override and new keywords. Versioning helps maintain compatibility between
base and derived classes as they evolve.
- User-Defined Conversions Sample: This sample shows how to define conversions to or from classes or structures, and how such conversions are used.
- Unsafe Code Sample: This sample demonstrates how to use unmanaged code (code using pointers) in C#.
- Threading Sample: This sample demonstrates the following threading techniques.
- Structs Sample: This sample presents the syntax and usage of structs. It also covers the important differences between classes and structs.
- Security Sample: This sample demonstrates how to modify security permissions through permission classes and permission attributes.
- Properties Sample: This sample shows how properties are an integral part of the C# programming language. It demonstrates how properties are declared and used.
- Platform Invoke Sample: This sample demonstrates how to call platform invokes (exported DLL functions) from C#.
- Partial Types Sample: This sample demonstrates the use of partial types, which enable a class or struct to be defined in two or more C# files.
- Operator Overloading Sample: This sample demonstrates how user-defined classes can overload operators.
- OLE DB Sample: This sample demonstrates how to use a Microsoft Access database from C#. It shows how you can create a dataset and add tables to it from a database.
The BugTypes.mdb database used in the sample program is a Microsoft Access 2000 .mdb file.
- Nullable Sample: This sample demonstrates the use of nullable types. This feature allows value types to have an uninitialized, or empty, state similar to how reference
types can be set to null.
- Libraries Sample: This sample shows how to create and use a DLL in C#.
- Indexers Sample: This sample shows how C# classes can declare indexers to provide array-like access to the classes.
- Indexed Properties Sample: This sample shows how C# classes can declare indexed properties to represent an array-like collection of different kinds of
- Hello World Sample: This sample shows several versions of a Hello World program in C#.
- Generics Sample (C#): This sample shows how to create a custom generic list class with a single type parameter, and how to implement IEnumerable<> to enable
foreach iteration over the contents of the list.
- Explicit Interface Implementation Sample: This sample demonstrates how to explicitly implement interface members and how to access those members from interface
- Events Sample: This sample shows how to declare, invoke, and configure events in C#.
- Delegates Sample: This sample demonstrates the delegate types. It shows how to map delegates to static and instance methods, and how to combine them to create multicast
- Conditional Methods Sample: This sample demonstrates conditional methods, which provide a powerful mechanism by which calls to methods can be included
or omitted depending on whether a symbol is defined.
- Command Line Parameters Sample: This sample shows how the command line can be accessed and two ways of accessing the array of command-line parameters.
- COM Interop Part 2 Sample: This sample demonstrates using a C# server with a C++ COM client.
- COM Interop Part 1 Sample: This sample demonstrates how a C# program can interoperate with an unmanaged COM component.
- Collection Classes Sample: This sample shows how to implement a collection class that can be used with the foreach statement.
- Attributes Sample: This sample demonstrates how to create custom attribute classes, use them in code, and query them through reflection.
- Arrays Sample: This sample describes and shows how arrays work in C#.
- Anonymous Delegates Sample: This sample demonstrates using an anonymous delegate to calculate a salary bonus for an employee. Using an anonymous delegate
simplifies the program because there is no need to define a separate method.
We have created four sets of samples for you that are designed to help you learn LINQ quickly, and to act as a reference and guide for those with more advanced skills. A few handy utilities are also included.
Important: If the folder links below do not function, please follow the instructions above under
Unblocking Folder Links.
Getting Started with LINQ
- SampleQueries: This is the most important sample and contains about 500 examples of how to use each of the query operators in LINQ to Objects, LINQ to SQL,
LINQ to XML and LINQ to DataSet.
- SimpleLinqToObjects: The hello world of the LINQ samples. This shows you how easy it is to do simple queries of in-memory objects using LINQ to Objects.
- LinqToNorthwind: A basic example of how to use LINQ To SQL to query a database.
- SimpleLinqToXml: Get started using LINQ to XML.
- WinFormsDataBinding: Learn how to display LINQ queries with grids in a Windows form. It includes a one to many query example.
- XQuery: Another simple LINQ to XML sample query. This sample shows the minimal code necessary to write a LINQ to XML query
- LinqToXmlDataBinding: Bind LINQ to XML code to WPF controls.
Code to create LINQ queries at run time.
- Reflector: Use LINQ to right queries against the objects in your code using the Reflection APIs.
- RSS: This sample acts as a tiny web server that aggregates several RSS feeds
- SimpleLambdas: Several examples of how to write and use lambda expressions.
- WebServiceLinqProvider: A sample showing how to create a LINQ provider to query Terraserver, a web service offering geographical data.
Addins and Visualizers
- ExpressionTreeVisualizer: Create a visualization of an Expression Tree.
- QueryVisualizer: Allows LINQ to SQL developers to see the SQL for their query, and also to see the results of the query in a grid.
A Visual Studio addin that automatically converts XML markup to LINQ to XML code.
Note: Addins cannot be compiled or loaded within C# Express.
Libraries or Utilities
Contains XML files and a modified copy of the Northwind database for use by several of the other samples.
A utility for writing the output from a LINQ query to the screen in text mode.
- NorthwindMapping: Contains the ORM mapping created by the LINQ to SQL designer for the Northwind database that ships with these samples.
It is used by DynamicQuery and WinFormsDataBinding.
- Whitepapers: The following papers are stored in this directory in Word format:
- LINQ Project Overview
- LINQ to SQL Overview for C# and VB
- Standard Query Operators
- LINQ to XML Overview
LINQ Samples Database Connectivity Issue
The default connect string for the samples should work without modification if you have SQL Server Express installed on your development machine. Here is an alternate connect string that you can modify for your own purposes if you are not
using SQL Express but have SQL Server available. You will probably need to modify the word test wherever it appears in the sample connect string.
string connString = "server=test;database=northwind;user id=test;password=test";
string connString = "server=test;database=northwind;user id=test;password=test";
On some SQL Express installations, you may not have the rights to start a user instance. If you get an error message to that effect, try removing the clause ";user instance = true" from the connection string.
Be sure that the copies of the northwind database that you are accessing are not marked read only. If necessary, browse to the copy of NORTHWIND.MDF you are trying to access, right click on the file, choose properties. Clear the Read-only attribute.
On some slow machines, or when using a virtual pc, users have reported that some database applications give errors the first time they run. Try running the sample two or three times.
The customized copies of the Northwind database included with the samples ship without an LDF (log) file. This is done intentionally, so that the LDF and MDF files do not get out of sync. When you run a sample that uses the database, or attach
to the database from the Server Explorer inside Visual Studio, the LDF file will be created automatically. You may have trouble connecting to the MDF file if you are using the SQL Server Management Studio without first creating a log file.