C#

Speech Recognition and Synthesis with .NET 3.0

For a long time I've wanted to experiment over the .NET speech recognition and synthesis capabilities. I've seen the new vista speech recognition features and it sounds pretty robust, aside from negative experiences from our guys at Microsoft Redmond... This article will provide a simple tutorial on Speech Recognition and Synthesis with the .NET Framework.   There are a few projects I'm interested in doing for my new appartment, for controlling electrical devices and my Media Center, and solid speech recognition is indeed a very interesting and usefull feature.   - Help keep this blog alive -   WIndows XP and Windows Vista both have speech recognition engines built...

Performance optimization with CompiledQuery

During a programmer's lifetime, a top priority when developing new software is performance. New architecture is good, new technologies are good, but lets not be hasty: in this domain, "easier" and "new" sometimes leads to "slower". With this in mind, I've been recently investigating the Entity Framework general performance when performing queries against a database, whether by using Linq or Entity SQL.   So I've come across the CompiledQuery class. This class allows us to cache an Entity Query and reuse it in the following requests in order to increase query execution speed from there on. Its usage is as it follows:   IQueryable<MyEntity> list; using (EntityContext conn = new EntityContext()) {     var query...

Tip: C# 3.0 Extension Methods

Another great feature in c# 3.0 is Extension Methods. Extension Methods are created in a way that allow developers to inject them in other classes as being part of them. This article will provide an example of this.   As you know, the string type has many methods and the length property. But suppose that, for some reason, you wish string objects to have a new method available, say, to add a timestamp on the start of the string. This is where Extension Methods come in handy. An Extension Method that would allow us to do this, is the following:       1 using System;     2 using System.Collections.Generic;     3 using System.Linq;     4 using...

Applying a Watermark to an image with .NET and GDI+

In this article I'll be showing how to watermark a photo using .NET and, more specifically, GDI+ through the System.Drawing namespace. GDI stands for Graphic Device Interface, and basically consists of a class-based API for C/C++. It enables applications to use graphics and formatted text on both the video display and the printer. Applications based on the Microsoft Win32 API do not access graphics hardware directly. Instead, GDI interacts with device drivers on behalf of applications. In the .NET world, GDI is referred to as GDI+, and developing applications using this technology is now much easier. For this article I'll implement a small...

How to duplicate Entity Framework objects

I've recently come across a problem that I thought it would be simple to solve by using Entity Framework. I wanted  to duplicate an object  to make things easier for me and, ultimately, to the user, but I guess my assumption was incorrect. After some tests with reflection, google searches and a lot or brainstorming I've reached the conclusion that in Entity Framework there isn't a built-in or clear method that allows developers to clone objects and then persist the newly created object with some changes.    - Help keep this blog alive -   You've probably tried to clear EntityKeysValue members and got the readonly...

Refactoring tips: Extract Interface

In the previous article I've given you a little taste on Field Encapsulation on Visual Studio. In this next artiicle I'll demonstrate how to automatically generate a fully fledged interface from an already defined class with properties and methods in it.   Lets say you have the following TestClass class:       1 using System;     2 using System.Collections.Generic;     3 using System.Text;     4      5 namespace Example     6 {     7     class TestClass     8     {     9         public TestClass()    10         {}    11     12         public string ImplementMe()    13         {}    14     15         public void ImplementMeToo()    16         {}    17     }    18 }   And now let's say you want to make an interface out of these methods for other classes to implement them. You can do this by going to...

Refactoring tips: Field Encapsulation

The refactor feature in Visual Studio is great for many tasks, and I'll show a few of them in this article. This might be old stuff for some, but believe me when I say there are still a lot of folks out there waiting to be enlightened (just need to read some blogs and forums). Encapsulate Field is one of such features of the Visual Studio Refactoring module: If for some reason you can't or don't want to use an auto-implemented property, just declare a private field and let Visual Studio generate the Property for you.    Let's say you have a...

Checking for Nullable generic types

In this article I'll demonstrate how to validate if a determined Type is a Nullable type, and check its underlying type. We will also be able to convert a Nullable type into its underlying type. This is particularly useful when we're dealing with database nullable types such as Int32, in a O/R Mapping. I'm using this pretty much in these Entity Framework days... For starters, the best way to check if an object is of generic nullable type (notice that Nullable has a generic and non-generic kind) is by infering over the IsGenericType property of the Type class. If the return is true we can then compare...

XMLInclude and SOAPInclude explained

I've seen many users in blogs and forums around the web facing several dificulties when it comes to to webservices and encoded SOAP/XML data types. In a client application that references a web service, there might be occasions when you need to use a derived object that is defined on your web service assemblies, but isn't directly available to your application. This is because the Web Services Discovery Tool (Disco.exe) that generates code for XML Web services and XML Web service clients only serializes objects that are directly or indirectly used in its web method signatures.   - Help keep this...

Generic Dictionary and its foreach with keyvaluepair

Small code snipplet for you to cease using boxing once and for all :)     1 using System;     2 using System.Collections.Generic;     3 using System.Configuration;     4 using System.Data;     5 using System.Linq;     6 using System.Web;     7 using System.Web.Security;     8 using System.Web.UI;     9 using System.Web.UI.HtmlControls;    10 using System.Web.UI.WebControls;    11 using System.Web.UI.WebControls.WebParts;    12 using System.Xml.Linq;    13 using System.Text;    14     15 namespace AnExample    16 {    17     class Program    18     {    19         static void Main(string[] args)    20         {    21             Dictionary<int, string> names = new Dictionary<int, string>();    22             names.Add(1, "Vania");    23             names.Add(2, "Raquel");    24             names.Add(3, "Susana");    25     26             foreach (KeyValuePair<int, string> pair in names)    27             {    28                 int id = pair.Key;    29                 string name = pair.Value;    30     31                 Console.WriteLine("{0} has id {1}", name, id.ToString());    32             }    33         }    34     }    35 }

Full C# Archive