12 Replies - 3873 Views - Last Post: 08 October 2009 - 04:32 AM

#1 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

C# 4.0 - New features coming

Post icon  Posted 13 August 2009 - 09:06 AM

As we all (or most) know the .Net Framework 4.0 is in it's beta testing now, so I thought I would talk about some of the new features coming in version 4.0. Here is a small list (then we can discuss each) of new features coming for C#:
  • Dynamic lookup
  • Optional and named parameters (this is a cool feature)
  • Improved COM Interoperability
Named & Optional Parameters

Quote

Named and optional parameters are really two distinct features, but are often useful together. Optional parameters allow you to omit arguments to member invocations, whereas named arguments is a way to provide an argument using the name of the corresponding parameter instead of relying on its position in the parameter list.


Let's see how this will work, let's say I have the following Employee class:
public class Employee 
{ 
	public string FirstName { get; set; } 
	public string LastName { get; set; } 
	public string Qualification { get; set; } 
	public string MiddleName { get; set; } 
	
	public Employee(string firstName, string lastName) 
	{ 
		FirstName = firstName; 
		LastName = lastName; 
		Qualification = "N/A"; 
		MiddleName = string.Empty; 
	} 
	
	public Employee(string firstName, string lastName, string qualification) 
	{ 
		FirstName = firstName; 
		LastName = lastName; 
		Qualification = qualification; 
		MiddleName = string.Empty; 
	} 
	
	public Employee(string firstName, string lastName, string qualification, string middleName)		
	{			
		FirstName= firstName;			
		LastName= lastName;			
		Qualification= qualification;		   
		MiddleName= middleName		
	}	
}



As you can see I have 3 constructors to account for the various ways an employee can be instantiated. If I created this in C# 4.0 I could have a single parameters and utilize the new optional parameters avaiable in 4.0. The new class would look like this

public class Employee 
{ 
	public string FirstName { get; set; } 
	public string LastName { get; set; } 
	public string Qualification { get; set; } 
	public string MiddleName { get; set; } 
	
	public Employee(string firstName, string lastName, string qualification = "N/A", string middleName = "") 
	{ 
		FirstName = firstName; 
		LastName = lastName; 
		Qualification = qualification; 
		MiddleName = middleName; 
	} 
}



So as you can see this is a feature that will come in quite handy as we move forward with C# 4.0

Dynamic Lookup

Quote

Dynamic lookup allows you a unified approach to invoking things dynamically. With dynamic lookup, when you have an object in your hand you do not need to worry about whether it comes from COM, IronPython, the HTML DOM or reflection; you just apply operations to it and leave it to the runtime to figure out what exactly those operations mean for that particular object.


The Dynamic Type

Quote

C# 4.0 introduces a new static type called dynamic. When you have an object of type dynamic you can “do things to it” that are resolved only at runtime:


With the dynamic type you can finally call properties of your anonymous types outside their scope (I can see this getting seriously overused myself)> Consider this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
	class Program
	{
		static void Main(string[] args)
		{
			foreach (dynamic d in GetValues())
				Console.WriteLine("{0} is {1} years old", d.Name, d.Age);
		}

		static IEnumerable<dynamic> GetValues()
		{
			Dictionary<string, int> result = new Dictionary<string, int>();
			result["David"] = 30;
			result["Jennifer"] = 25;
			foreach (var item in result)
				yield return new { Name = item.Key, Age = item.Value };
		}
	}
}



This code yields an IEnumerable of an anonymous type, which are used outside the scope of the Main method. This is possible only with the use of the dynamic type.

Here's another example of the power of the dynamic type. Let's say I have the following code for setting the value of a property of an object, before I would have to use

Assembly asmLib= Assembly.LoadFile(@"C:\temp\DemoClass\bin\Debug\DemoClass.dll");
Type demoClassType = asmLib.GetType("DemoClass.DemoClassLib");
object demoClassobj= Activator.CreateInstance(demoClassType);
PropertyInfo pInfo= demoClassType.GetProperty("Name");
pInfo.SetValue(demoClassobj, "Adil", null);



This can be written like this when utilizing a dynamic type

Assembly asmLib= Assembly.LoadFile(@"C:\temp\DemoClass\bin\Debug\DemoClass.dll");
Type demoClassType = asmLib.GetType("DemoClass.DemoClassLib");
dynamic dynamicDemoClassObj = Activator.CreateInstance(demoClassType);
dynamicDemoClassObj.Name = "Adil";



See how the last 3 lines can be re-written with just 2 lines, allowing me to access the properties of any type outside it's original scope.

This is not meant to be an exhaustive look at the upcoming new features, but it's a starting point to discuss your favorite upcoming features in C# 4.0

Is This A Good Question/Topic? 0
  • +

Replies To: C# 4.0 - New features coming

#2 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: C# 4.0 - New features coming

Posted 13 August 2009 - 09:27 AM

View PostPsychoCoder, on 13 Aug, 2009 - 10:06 AM, said:

With the dynamic type you can finally call properties of your anonymous types outside their scope


I don't think I like that one. It would depend on how much the compiler protects me from my own stupidity; but outside the assembly it can't. The problems it solves are trivial, the problems it creates could me monumental.

If I wanted duck typing in my C#, I'd use Python, or Ruby, or Javascript, or Lua, or...

This post has been edited by baavgai: 13 August 2009 - 09:27 AM

Was This Post Helpful? 0
  • +
  • -

#3 Core   User is offline

  • D.I.C Lover
  • member icon

Reputation: 785
  • View blog
  • Posts: 5,101
  • Joined: 08-December 08

Re: C# 4.0 - New features coming

Posted 13 August 2009 - 11:10 AM

I really like optional parameters. Sometimes overloading methods is not really the right and optimal solution, so optional parameters come handy. I know it was for a while already in VB.NET but I am glad that it will now be available in C#.
Was This Post Helpful? 0
  • +
  • -

#4 prajayshetty   User is offline

  • D.I.C Addict
  • member icon

Reputation: 31
  • View blog
  • Posts: 920
  • Joined: 27-April 07

Re: C# 4.0 - New features coming

Posted 13 August 2009 - 12:39 PM

good i want both java and C# rocks since i dont know vb.net :P
Was This Post Helpful? 0
  • +
  • -

#5 killnine   User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 161
  • Joined: 12-February 07

Re: C# 4.0 - New features coming

Posted 14 August 2009 - 05:22 AM

View Postbaavgai, on 13 Aug, 2009 - 08:27 AM, said:

View PostPsychoCoder, on 13 Aug, 2009 - 10:06 AM, said:

With the dynamic type you can finally call properties of your anonymous types outside their scope


I don't think I like that one. It would depend on how much the compiler protects me from my own stupidity; but outside the assembly it can't. The problems it solves are trivial, the problems it creates could me monumental.

If I wanted duck typing in my C#, I'd use Python, or Ruby, or Javascript, or Lua, or...


QFT. I can see this getting people into sticky situations. i can't, personally, see a whole lot of use in this.

Optional parameters, though.....that's a different story.
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: C# 4.0 - New features coming

Posted 14 August 2009 - 05:35 AM

<Sarcasm>
Optional and Named Parameters wow wee!
</Sarcasm>

C# is just getting them, vb.net had them for yonks.
Was This Post Helpful? 0
  • +
  • -

#7 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: C# 4.0 - New features coming

Posted 14 August 2009 - 07:41 AM

View PostAdamSpeight2008, on 14 Aug, 2009 - 06:35 AM, said:

<Sarcasm>
Optional and Named Parameters wow wee!
</Sarcasm>

C# is just getting them, vb.net had them for yonks.


Optional parameters are achieved through operator overloading. That C# is closing in on VB.NET, that always had crap OO constructs, is yet another red flag.
Was This Post Helpful? 0
  • +
  • -

#8 Amrykid   User is offline

  • D.I.C Lover
  • member icon

Reputation: 151
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Re: C# 4.0 - New features coming

Posted 14 August 2009 - 09:38 AM

View PostPsychoCoder, on 13 Aug, 2009 - 09:06 AM, said:

The Dynamic Type

Quote

C# 4.0 introduces a new static type called dynamic. When you have an object of type dynamic you can “do things to it” that are resolved only at runtime:


With the dynamic type you can finally call properties of your anonymous types outside their scope (I can see this getting seriously overused myself)> Consider this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
	class Program
	{
		static void Main(string[] args)
		{
			foreach (dynamic d in GetValues())
				Console.WriteLine("{0} is {1} years old", d.Name, d.Age);
		}

		static IEnumerable<dynamic> GetValues()
		{
			Dictionary<string, int> result = new Dictionary<string, int>();
			result["David"] = 30;
			result["Jennifer"] = 25;
			foreach (var item in result)
				yield return new { Name = item.Key, Age = item.Value };
		}
	}
}



This code yields an IEnumerable of an anonymous type, which are used outside the scope of the Main method. This is possible only with the use of the dynamic type.

Here's another example of the power of the dynamic type. Let's say I have the following code for setting the value of a property of an object, before I would have to use

Assembly asmLib= Assembly.LoadFile(@"C:\temp\DemoClass\bin\Debug\DemoClass.dll");
Type demoClassType = asmLib.GetType("DemoClass.DemoClassLib");
object demoClassobj= Activator.CreateInstance(demoClassType);
PropertyInfo pInfo= demoClassType.GetProperty("Name");
pInfo.SetValue(demoClassobj, "Adil", null);



This can be written like this when utilizing a dynamic type

Assembly asmLib= Assembly.LoadFile(@"C:\temp\DemoClass\bin\Debug\DemoClass.dll");
Type demoClassType = asmLib.GetType("DemoClass.DemoClassLib");
dynamic dynamicDemoClassObj = Activator.CreateInstance(demoClassType);
dynamicDemoClassObj.Name = "Adil";



See how the last 3 lines can be re-written with just 2 lines, allowing me to access the properties of any type outside it's original scope.

This is not meant to be an exhaustive look at the upcoming new features, but it's a starting point to discuss your favorite upcoming features in C# 4.0

Or, you could just use
Assembly asmLib= Assembly.LoadFile(@"C:\temp\DemoClass\bin\Debug\DemoClass.dll");
Type demoClassType = asmLib.GetType("DemoClass.DemoClassLib");
[b]object[/b] dynamicDemoClassObj = Activator.CreateInstance(demoClassType);
dynamicDemoClassObj.Name = "Adil";


This post has been edited by Amrykid: 14 August 2009 - 09:39 AM

Was This Post Helpful? 0
  • +
  • -

#9 SwiftStriker00   User is offline

  • No idea why my code works
  • member icon

Reputation: 440
  • View blog
  • Posts: 1,618
  • Joined: 25-December 08

Re: C# 4.0 - New features coming

Posted 15 August 2009 - 10:21 AM

View PostAdamSpeight2008, on 14 Aug, 2009 - 06:35 AM, said:

<Sarcasm>
Optional and Named Parameters wow wee!
</Sarcasm>

C# is just getting them, vb.net had them for yonks.



Ya Ya, VB.net is god we know. But im excited just out of sheer convince, optional parameters are awesome.
Not to mention that I read that MS actually never intended to put them in, and I guess they buckled from
user emails
Was This Post Helpful? 0
  • +
  • -

#10 W3bDev   User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 379
  • Joined: 15-March 09

Re: C# 4.0 - New features coming

Posted 25 August 2009 - 11:20 AM

View PostSwiftStriker00, on 15 Aug, 2009 - 09:21 AM, said:

View PostAdamSpeight2008, on 14 Aug, 2009 - 06:35 AM, said:

<Sarcasm>
Optional and Named Parameters wow wee!
</Sarcasm>

C# is just getting them, vb.net had them for yonks.



Ya Ya, VB.net is god we know. But im excited just out of sheer convince, optional parameters are awesome.
Not to mention that I read that MS actually never intended to put them in, and I guess they buckled from
user emails



LOL... VB.net can keep its crazy syntax... worked with that for about 6 months now, and I'm very glad to have my VB.NET requirement finished for my degree lol.

Give me brackets and strongly typed structures, or give me death!

On a side note, dynamic variables, eh, probably will encourage some newbies to be lazy, and optional parameters, that's awesome.

This post has been edited by W3bDev: 25 August 2009 - 11:21 AM

Was This Post Helpful? 0
  • +
  • -

#11 Dalokin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-October 09

Re: C# 4.0 - New features coming

Posted 08 October 2009 - 01:26 AM

Hm.. the dynamic vars will probably bring a lot of extra errors and debugging hours if not used properly.

Quote

<Sarcasm>
Optional and Named Parameters wow wee!
</Sarcasm>

C# is just getting them, vb.net had them for yonks.

I agree on that, i wonder if M$ couldn't have thought of some better features instead of taking vb.net as an example.
Was This Post Helpful? 0
  • +
  • -

#12 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: C# 4.0 - New features coming

Posted 08 October 2009 - 03:45 AM

View PostDalokin, on 8 Oct, 2009 - 04:26 AM, said:

Hm.. the dynamic vars will probably bring a lot of extra errors and debugging hours if not used properly.

Quote

<Sarcasm>
Optional and Named Parameters wow wee!
</Sarcasm>

C# is just getting them, vb.net had them for yonks.

I agree on that, i wonder if M$ couldn't have thought of some better features instead of taking vb.net as an example.


why wouldn't Microsoft take features that are in vB.Net. It has features that Microsoft's flagship language(c#) doesn't. And many of the said features have been requested by the c# community for years.
Was This Post Helpful? 0
  • +
  • -

#13 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: C# 4.0 - New features coming

Posted 08 October 2009 - 04:32 AM

So you never do communications to office.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1