Custom Software Blog Custom Software Consulting

C# Generics Summary: Methods, Types and Specifying Generic Type Parameter Constraints

by Aristo Setiawan 12/2/2009 3:55:00 AM

This article was written based on a presentation given by Timothy Bussmann.

Generics in C# are classes, structures, interfaces, and methods that have type parameters for one or more of the types that they use. For example, instead of using a collection class that allows any type, we can use a generic collection class and define the specific type(s) for the collection. The primary use case of generics is indeed to create type-safe generic collections and enumerators. Generics can also be used in lambda expressions, delegates and utility code.

There are a number of benefits of using generics. First, it promotes code reusability by writing code that is independent of the type(s) used for generic type parameter(s). Performance over object collections is improved because no runtime casts or boxing conversions need to be performed. Lastly, generics promote type safe programming and allow type inference to determine type parameters without requiring explicit specification.

Lambda Expressions and Func<>

A Lambda Expression is an anonymous function that contains an expression or statements is essentially a shorter way of writing a delegate. The Func<T, TResult> delegates use type parameters to define how many and, if necessary, the type(s) of input parameters and the return type of the delegate. Following are a few examples below on how lambda expressions are used in .NET:

  1. The following code:
    Clicked += delegate(object sender, EventArgs args) { DoSomething(); }
    Can be replaced with:
    Clicked += (sender, args) => { DoSomething(); }
  2. It is used frequently in LINQ:
    ordersTable.Where(order => order.IsPending);
    ordersTable.Select(order =>
        new { order.OrderID, order.Name, order.IsPending } );

Note that even though arguments are not specified, they remain type-safe. The types are simply inferred based on program information available to the compiler.

Constraints

When defining a generic class, you can apply restrictions to what types can be used as the arguments. These restrictions are called Constraints and applied by using where keyword. It is used to guarantee that a call to this method conforms to these constraints, and thus access the functionality defined by the constraints. These are the different types of constraints:

More...

Currently rated 3.5 by 2 people

  • Currently 3.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Software Development

About Microsoft Silverlight

by Carlos Salaverria 7/14/2009 7:06:00 AM

This article was written based on a presentation given by Brad Harris.

Silverlight is a Rich Internet Application framework developed by Microsoft. Like other RIA frameworks such as Adobe Flash, it enables developers to create applications that have a consistent appearance and functionality across different web browsers and operating systems. Silverlight provides users with a rich experience, with built-in support for vector graphics, animation, and audio and video playback.

Although Silverlight is still a relatively new technology, it has already been used for live streaming of high-profile events such as the 2008 Beijing Olympics and the 2009 Presidential Inauguration. It is also being used by major sports organizations to broadcast live content.

To run Silverlight applications, users must install a browser plug-in, which provides a runtime environment that implements a subset of the .NET Framework. Currently, Silverlight is available on Internet Explorer, Firefox, and Google Chrome for Microsoft Windows and on Safari and Firefox for Mac OS X.

To start building Silverlight applications, developers need to install Silverlight Tools for Visual Studio 2008 SP1. This add-on is available for download from the official Microsoft Silverlight website: http://silverlight.net. It is compatible with both Visual Studio 2008 and Visual Web Developer 2008 Express.

The development experience is similar to ASP.NET – applications are implemented using markup and code-behind. One significant difference is the markup language. Instead of HTML, Silverlight uses a UI definition language known as XAML (eXtensible Application Markup Language). Although there are graphical tools such as Expression Blend that make it easier for designers to produce XAML code, many developers prefer to write XAML by hand. Regardless of the design tool used, learning XAML is essential for developing Silverlight applications.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Software Development

How To Manage Database Development More Efficiently

by Brad Harris 6/8/2009 5:18:00 AM

This article was written based on a presentation given by Eric Carlson.

VS Team System 2008 Database Edition GDR is a component of the Visual Studio Team System suite of tools. It was formerly known as “DB Pro”. As the General Distribution Release, it is a post-service pack 1 release. It incorporates several of the features slated for Visual Studio 2010 “Rosario”. It also has SQL Server 2008 support.

Some of the key features of this Team System release are listed below:

  1. T-SQL Editor
    The T-SQL Editor support ad hoc queries within Visual Studio without having to rely on SQL Management Studio (finally!). Also like Management Studio, Team System 2008 Database Edition supports client statistics, results as text and results as file. Unfortunately there is no Intellisense for SQL Server 2008 built in.
  2. Database Projects
    This tool supports off-line schema development in a file format denoted by *.dbschema files. Being able to develop against non-live files is a huge advantage for shared database development. Version control is also supported to capture schema changes as they are developed. Within the Database Projects, compile-time schema validation and static code analysis are presented as warnings. Refactoring and deployment is also made easier by the Database Project tools.
  3. Server Projects
    Server Projects model both shared and server-level objects. This enables a team to define a standard configuration for their SQL servers and reference it within their Database Projects.


  4. More...

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

Software Development

Test Driven Development in .NET

by Eric Carlson 5/18/2009 6:22:00 AM

This article was written based on a presentation given by Aristo Setiawan.

Test-driven development is a development pattern which is characterized by the use of pre-written test cases which are continuously validated via an iterative, incremental development process.

The basic process is:

  1. Write the test.
  2. Write the code which is tested by the test from step 1.
  3. Validate that the new test and all previously implemented tests run successfully.

The process is repeated for each test until the code is fully implemented.

Benefits of test-driven development include:

  • Forces simplicity in the code by breaking down complex tasks into individual subtasks.
  • Ensures that the developer understands the intent of the code before writing it.
  • Allows defects to be detected early in the development lifecycle.
  • Prevents regression by identifying breaking changes prior to release of new versions of the code.

It is important for buyers of software to understand that a solution developed with automated testing (whether or not it is a result of test-driven development or build-then-test development) will have a higher up-front cost. However, automated testing can be expected to pay for itself over the total software asset lifecycle through reduced occurrence of bugs and the limiting of regression.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

Software Development

Why use Linq instead of SQL?

by Tim Bussmann 4/3/2009 7:49:00 AM

To give you an idea of what Linq looks like, and how it compares to SQL code, below are some very simple queries in both SQL and Linq. This assumes a Customer table with columns ID and Name: 

SQL Linq
SELECT UPPER (c.Name)
FROM Customer AS c
WHERE c.Name LIKE '%a%'
ORDER BY LEN(c.Name)
from c in Customers
where c.Name.Contains ("a")
orderby c.Name.Length
select c.Name.ToUpper ()

Strongly Typed Queries
Linq queries are all strongly typed since classes are autogenerated to match your database schema. As a result, you also get intellisense all the way through composing a query. For example, in the above query, after typing "c." you would get an intellisense popup showing the properties of the Customer class, just like regular coding in Visual Studio. This also means that if you change the name of a table or column in the database, you no longer have to hunt through all your stored procedures to change references to the old name, instead you can just follow the trail of build errors in your code. You can even be tricky by doing name refactor from the old name to the new name before updating the schema.

More...

Currently rated 4.3 by 3 people

  • Currently 4.333333/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Software Development

Powered by BlogEngine.NET 1.4.5.0