日期:2014-05-20 浏览次数:20975 次
IEnumerable<string> split(string, source, char c) { string s = ""; for (int i = 0; i < source.Length; i++) { if (source[i] == c) { yield return s; s = ""; } else { s += new string(new char[] { source[i] }); } } if (s != "") yield return s; }
foreach (string s in split("hello world csharp", ' ')) { Console.WriteLine(s); }
Sub Main() For Each number As Integer In SomeNumbers() Console.Write(number & " ") Next ' Output: 3 5 8 Console.ReadKey() End Sub Private Iterator Function SomeNumbers() As System.Collections.IEnumerable ' Use multiple yield statements. Yield 3 Yield 5 Yield 8 End Function
// Synchronous version of a method that downloads the resource that a URI // links to and accesses its content. private byte[] GetByteArray(Uri currentURI) { // Declare an HttpClient object and increase the buffer size. HttpClient client = new HttpClient() { MaxResponseContentBufferSize = 1000000 }; // The Get method returns an HttpResponseMessage that contains the // content of the resource along with other information. HttpResponseMessage httpRM = client.Get(currentURI); // Use ReadAsByteArray to access the content as a byte array. return httpRM.Content.ReadAsByteArray(); } // Asynchronous version of the same method. private async Task<byte[]> GetByteArrayAsync(Uri currentURI) { // Declare an HttpClient object and increase the buffer size. HttpClient client = new HttpClient() { MaxResponseContentBufferSize = 1000000 }; // The GetAsync method returns a Task(Of T), where T is an HttpResponseMessage. HttpResponseMessage httpRM = await client.GetAsync(currentURI); // Use ReadAsByteArray to access the content as a byte array. return httpRM.Content.ReadAsByteArray(); }
[<Generate>] type Northwind = ODataService<"http://services.odata.org/Northwind/Northwind.svc"> let db = new Northwind.NorthwindEntities() // A query expression. let query1 = query { for customer in db.Customers do select customer } query1 |> Seq.iter (fun customer -> printfn "Company: %s Contact: %s" customer.CompanyName customer.ContactName)