Tuesday, November 10, 2009

Yield statement in C#

Yield statement is one of most interesting statement in the C# 2.0. I have heard about lot of yield statement but today i have learned about yield statement. Yield statement can be used to return multiple object from a method while retaining its state. You can get item in sequence with help of yield.Let take a simple example to see the power or yield statement.

Let's create one example which will help you the understand how its works Let create a function will return the square each time this function is called.



Code Snippet



  1. public static IEnumerable<int> Square(int min, int max)
  2. {
  3. for (int i = min; i < max; i++)
  4. {
  5. yield return i*i;
  6. }
  7. }





Now each time this method is called it will return the square of current value within a given range and its also maintains the state between calls. Let create for each loop to call the the square function above.



Code Snippet



  1. foreach (int i in Square(1, 10))
  2. {
  3. Response.Write(i.ToString() + " ");
  4. }





And here will be the output in the browser.

image

So it will create IEnumerable without implementing any interface and lesser code.

Happy Programming..

Technorati Tags: ,,

kick it on DotNetKicks.com
Shout it
Share:
Sunday, November 8, 2009

C# 4.0 - Optional Parameters

With .net framework 4.0 and C# 4.0 also introduces a new feature called optional parameters it was there in VB since visual basic 6.0. But it is now available in the C# with 4.0 version. Optional parameter will be become very handy when you don't want to pass some parameter and just you need the default value. For that Till C# 3.0 we need to create overloaded functions where we creating function with different argument with same function name. Let's see how its works

Let's go through a simple scenario we will create function with simple response.write which will print a string which will be optional argument.



Code Snippet


  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. PrintMessage();
  4. PrintMessage("This is message to print passed as argument");
  5. }
  6. private void PrintMessage(string MessageToPrint="This is default message to print")
  7. {
  8. Response.Write(MessageToPrint);
  9. }




See the above example in that we have created one created once function PrintMessage which will print message on web page thorough Response.Write. And in the Page_Load event we called two times first one is which without argument which will print default message and another one which will have message an message argument which will print message which we have passed. Lets run that application and following out put will produced.

PrintMessage

So that's it it will work like this if you passed argument the it will use argument or otherwise its will use the default argument. This will become handy and you don't need to write overloaded function with default value.

Happy Programming ...

Technorati Tags: ,,
Shout it
Share:
Saturday, November 7, 2009

New publish setting dialog in visual studio 2010 beta 2.

Those who are working with asp.net will also know about the publishing site to directly with publish dialog. This was introduced with visual studio 2005 and till now it is one of the best way to publish your asp.net site with only required files. Visual studio 2010 is not a exception but the publish dialog is enhanced with more feature. Now the its directly given in the toolbar of visual studio 2010 like following.

Publishsetting

Once you select the new the following dialog will appear.

PublishDialog

In visual studio 2010 publish setting dialog has more options then earlier version. Now you can also save publish profile also and this profile will appear in the publish setting and by choosing that you can directly publish your site from the publish button with same setting you set for this profile.

The publish setting dialog also has more options then earlier version of visual studio. It is having four option.

  1. MSDeploy Publish
  2. Ftp
  3. FileSystem
  4. FPSE

MSDeploy Publish:

With this option you can directly deploy your site on Remote server IIS with MSDeploy tool .This is one of coolest thing in click once deployment. It can also deploy all the IIS setting, content of the website,registry setting for website,required assembly etc. For more details about this please visit following link:

http://vishaljoshi.blogspot.com/2009/02/web-deployment-with-vs-2010-and-iis.html

FTP:

This is the same as visual studio 2008. You can directly publish your site on remote server with ftp protocol. If you have the username and password for ftp protocol then it is the easiest thing to publish your site on remote server.

File System:

This will create a published version of your webaplication on folder which you specified and then you can copy it whatever location you want. If you are having VPN of remote server then you can directly copy the publish version of you web application directly on specified location

FPSE:

FPSE means front page server extension.This is the oldest one which were introduced with the Internet was fairly young, to support things like hit counters, email components. Some developers that develop with FrontPage use these. It also provide way to publish content.

Shout it
Share:

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews