Saturday, October 31, 2009

.net 3.5 Extension Methods, Its a syntactiv sugar.

Using extension methods and object oriented techniques, this is the best example i have come across.

public static void DoIt()
{
var someMeeting = Meeting.Between("jack").And("jill").At(8.OClock().Tommorrow());
}

Other than some basic Oops inheritance, what is used here is Extension methods which i would put here. So, in a way extension methods help in writing a more compact code (syntactic sugar) , which is not always possible using inheritance, polymorphism.


public static class Extensions
{
public static TimeSpan OClock(this int hour)
{
return new TimeSpan(hour, 0, 0);
}

public static DateTime Tommorrow(this TimeSpan time)
{
return new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, time.Hours, time.Minutes, time.Seconds);
}
}

Wednesday, October 28, 2009

Sending mails from C# with embedded images.

//Sample Code from MCTS 70-536 Kit

// Create the HTML message body
// Reference embedded images using the content ID

string htmlBody =
"[html][body][h1]Picture[/h1]img src=\"cid:Pic1\"][/body][/html]";

AlternateView avHtml = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
// Create a LinkedResource object for each embedded image
LinkedResource pic1 = new LinkedResource("pic.jpg", MediaTypeNames.Image.Jpeg);
pic1.ContentId = "Pic1";
avHtml.LinkedResources.Add(pic1);
// Create an alternate view for unsupported client
sstring textBody ="You must use an e-mail client that supports HTML messages";
AlternateView avText = AlternateView.CreateAlternateViewFromString(textBody, null, MediaTypeNames.Text.Plain);
// Add the alternate views instead of using MailMessage.Body
MailMessage m = new MailMessage();
m.AlternateViews.Add(avHtml);
m.AlternateViews.Add(avText);
// Address and send the message
m.From = new MailAddress("lance@contoso.com", "Lance Tucker");
m.To.Add(new MailAddress("james@contoso.com", "James van Eaton"));
m.Subject = "A picture using alternate views";
SmtpClient client = new SmtpClient("smtp.contoso.com");
client.Send(m);

Monday, October 26, 2009

Difference between Trace and Debug - C#

The Trace class functions almost identically to the Debug class. However, calls to Trace are executed in both Debug and Release builds. Therefore, use the Debug class to writemessages only in the Debug build, and use the Trace class to write messages regardlessof the build type.

Passing arguments as 'ref' in C#.

What confuses me about passing a variable as reference (ref) is the concept of value types and reference types in C#.
Since, reference types are always passed by reference, why would we need to pass a reference type as 'ref' argument?

The difference is that passing an argument as 'ref' allows the method to change the reference also (memory address stored in the variable), which can not be done otherwise. A reference type allows the calling method to change the state of the instance (some fields), while passing an argument as 'ref' allows the calling method to assign a new instance to the variable.

Sample Code:

public class PlayWithRef {
public class Employee {
public string Name { get; set; }
public override string ToString() {
return Name;
}
}
public static void DoIt() {
var emp=new Employee{Name="Rina"};
ModifyMe(emp);
Console.WriteLine(emp);
ModifyMeAlso(ref emp);
Console.WriteLine(emp);
}
public static void ModifyMe(Employee str) {
str=new Employee { Name="NonRef Rina" };
}
public static void ModifyMeAlso(ref Employee str) {
str=new Employee{Name="Ref Rina"};
}
}

Essentially what happens is, passing an argument sends the heap memory address and the argument can access the instance lying in that address and can modify its instance/fields but can not make the original variable point to a new memory address, which is possible when the variable is send as 'ref'.