mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-24 01:51:38 +00:00
Merge pull request #1477 from jdpearce/master
Fixed the custom indexer example (setter return type is void)
This commit is contained in:
commit
f4cfe81a21
@ -6,6 +6,7 @@ contributors:
|
|||||||
- ["Melvyn Laïly", "http://x2a.yt"]
|
- ["Melvyn Laïly", "http://x2a.yt"]
|
||||||
- ["Shaun McCarthy", "http://www.shaunmccarthy.com"]
|
- ["Shaun McCarthy", "http://www.shaunmccarthy.com"]
|
||||||
- ["Wouter Van Schandevijl", "http://github.com/laoujin"]
|
- ["Wouter Van Schandevijl", "http://github.com/laoujin"]
|
||||||
|
- ["Jo Pearce", "http://github.com/jdpearce"]
|
||||||
filename: LearnCSharp.cs
|
filename: LearnCSharp.cs
|
||||||
---
|
---
|
||||||
|
|
||||||
@ -418,6 +419,42 @@ on a new line! ""Wow!"", the masses cried";
|
|||||||
// Item is an int
|
// Item is an int
|
||||||
Console.WriteLine(item.ToString());
|
Console.WriteLine(item.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// YIELD
|
||||||
|
// Usage of the "yield" keyword indicates that the method it appears in is an Iterator
|
||||||
|
// (this means you can use it in a foreach loop)
|
||||||
|
public static IEnumerable<int> YieldCounter(int limit = 10)
|
||||||
|
{
|
||||||
|
for (var i = 0; i < limit; i++)
|
||||||
|
yield return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
// which you would call like this :
|
||||||
|
public static void PrintYieldCounterToConsole()
|
||||||
|
{
|
||||||
|
foreach (var counter in YieldCounter())
|
||||||
|
Console.WriteLine(counter);
|
||||||
|
}
|
||||||
|
|
||||||
|
// you can use more than one "yield return" in a method
|
||||||
|
public static IEnumerable<int> ManyYieldCounter()
|
||||||
|
{
|
||||||
|
yield return 0;
|
||||||
|
yield return 1;
|
||||||
|
yield return 2;
|
||||||
|
yield return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
// you can also use "yield break" to stop the Iterator
|
||||||
|
// this method would only return half of the values from 0 to limit.
|
||||||
|
public static IEnumerable<int> YieldCounterWithBreak(int limit = 10)
|
||||||
|
{
|
||||||
|
for (var i = 0; i < limit; i++)
|
||||||
|
{
|
||||||
|
if (i > limit/2) yield break;
|
||||||
|
yield return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void OtherInterestingFeatures()
|
public static void OtherInterestingFeatures()
|
||||||
{
|
{
|
||||||
@ -753,7 +790,7 @@ on a new line! ""Wow!"", the masses cried";
|
|||||||
|
|
||||||
// It's also possible to define custom Indexers on objects.
|
// It's also possible to define custom Indexers on objects.
|
||||||
// All though this is not entirely useful in this example, you
|
// All though this is not entirely useful in this example, you
|
||||||
// could do bicycle[0] which yields "chris" to get the first passenger or
|
// could do bicycle[0] which returns "chris" to get the first passenger or
|
||||||
// bicycle[1] = "lisa" to set the passenger. (of this apparent quattrocycle)
|
// bicycle[1] = "lisa" to set the passenger. (of this apparent quattrocycle)
|
||||||
private string[] passengers = { "chris", "phil", "darren", "regina" };
|
private string[] passengers = { "chris", "phil", "darren", "regina" };
|
||||||
|
|
||||||
@ -764,7 +801,7 @@ on a new line! ""Wow!"", the masses cried";
|
|||||||
}
|
}
|
||||||
|
|
||||||
set {
|
set {
|
||||||
return passengers[i] = value;
|
passengers[i] = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -880,7 +917,7 @@ on a new line! ""Wow!"", the masses cried";
|
|||||||
## Topics Not Covered
|
## Topics Not Covered
|
||||||
|
|
||||||
* Attributes
|
* Attributes
|
||||||
* async/await, yield, pragma directives
|
* async/await, pragma directives
|
||||||
* Web Development
|
* Web Development
|
||||||
* ASP.NET MVC & WebApi (new)
|
* ASP.NET MVC & WebApi (new)
|
||||||
* ASP.NET Web Forms (old)
|
* ASP.NET Web Forms (old)
|
||||||
|
Loading…
Reference in New Issue
Block a user