How to use Yield Return

In a yield return statement, expression is evaluated and returned as a value to the enumerator object; expression has to be implicitly convertible to the yield type of the iterator.
The yield statement can only appear inside an iterator block, which can be implemented as the body of a method, operator, or accessor. The body of such methods, operators, or accessors is controlled by the following restrictions:
  • Unsafe blocks are not allowed.
  • Parameters to the method, operator, or accessor cannot be ref or out.
  • A yield return statement cannot be located anywhere inside a try-catch block. It can be located in a try block if the try block is followed by a finally block.
  • A yield break statement may be located in a try block or a catch block but not a finally block.
A yield statement cannot appear in an anonymous method

Sample

 namespace Yield_Return  
{
class Program
{
static void Main(string[] args)
{
// Compute two with the exponent of 30.
foreach (int value in ComputePower(2, 30))
{
Console.Write(value);
Console.Write(" ");
}
Console.WriteLine();
Console.ReadLine();
}
public static IEnumerable ComputePower(int number, int exponent)
{
int exponentNum = 0;
int numberResult = 1;
//
// Continue loop until the exponent count is reached.
//
while (exponentNum < exponent)
{
//
// Multiply the result.
//
numberResult *= number;
exponentNum++;
//
// Return the result with yield.
//
yield return numberResult;
}
}
}
public class Yield {
public IEnumerable GetMergedCount(string[] count, string[] count1)
{
foreach (string item in count)
{
string counts = string.Empty;
foreach (string item1 in count1)
{
if (item1.Equals(item))
{
counts =counts+ item1.ToString();
yield return counts;
}
}
}
}
}
}

Substitution Control

How you can use SubstitutionControl  in Asp.net?

Use the Substitution control to specify a section of an output-cached Web page where you want to display dynamic content. The Substitution control offers a simplified solution to partial page caching for pages where the majority of the content is cached. You can output-cache the entire page, and then use Substitution controls to specify the parts of the page that are exempt from caching. Cached regions execute only once and are read from the cache until the cache entry expires or is purged. Dynamic regions execute every time that the page is requested. This caching model simplifies the code for pages that are primarily static, because you do not have to encapsulate the sections to cache in Web user controls. For example, this caching model is useful in a scenario where you have a page that contains static content, such as news stories, and an AdRotator control that displays advertisements. The news stories do not change frequently, which means that they can be cached. However, every time that a user requests the page, you want to display a new advertisement. The AdRotator control directly supports post-cache substitution and renders a new advertisement every time that the page posts back, regardless of whether the page is cached.

Aspx Code

   










Asp Label



Substitution-Control

<asp:Substitution runat="server"
ID="Substitution1"
MethodName="GetTime" />









Code Behind (.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Authentication_Authorization
{
public partial class Substitution_control : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
}
public static String GetTime(HttpContext context)
{
return DateTime.Now.ToString();
}
}
}