C# 4.0 is coming with a great new feature, Consume First. You can consume any type first and define it after consuming. Lets jump straight into the coding, and see how does it work. Start by creating a Console Application, and type a class name, that is not declared, in the main method. I used the ConsumeFirstClass for this example. The intellisence doesn't show the class:
when you type the assignment operator, =, and new the intellisense starts showing this class:
Remember the class name is underlined because we don't have defined it. Now write an arbitrary method, I wrote ConsumeFirstMethod:
Our class object is showing in the intellisense. And pass in the string parameter, you can pass any thing, the method takes as parameter.
Now place the cursor on the ConsumeFirstClass variable declaration and press Ctrl + . to show the available options:
Select "Generate class for 'ConsumeFirstClass' ". Now visual studio 2010 places a new file named ConsumeFirstClass.cs.
Now go the the method call to ConsumeFirstMethod, press Ctrl + . (dot) and select Generate method stub ...
The class definition looks something like this:
The visual studio 2010 class generator is smart enough that it determined our method signature, it takes a string and returns an int.
Now remove the throw statement and place a return statement:
And here is the output...
Ohhhhhhhhhh......... I think something went wrong. Let me fix it. Here is the fixed output:
That is for now. You can declare any type, enum, struct, class by this method. Yes, You can write a class AtomBomb and define a method Blast using Consume First.