Skip to content

Instantly share code, notes, and snippets.

@clauderoy790
Created August 1, 2021 03:26
Show Gist options
  • Save clauderoy790/580b246b9761b1fc4f2080d3965db6fd to your computer and use it in GitHub Desktop.
Save clauderoy790/580b246b9761b1fc4f2080d3965db6fd to your computer and use it in GitHub Desktop.
Simple LINQ, lambda and string formatting example.
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.VisualBasic.FileIO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Linq;
namespace csharp_test
{
class Program
{
class Book { //Inner Class
public Book(string title, string publisher) {
this.Title = title;
this.Publisher = publisher;
}
public int Id { get; set; }
public string Title { get; set; }
public string Publisher { get; set; }
public override string ToString()
{
return $"Title:{Title}, Publisher:{Publisher}";
}
}
static void Main(string[] args)
{
List<Book> books = new List<Book>();
books.Add(new Book("A Book Title1","Publisher"));
books.Add(new Book("A Book Title0","Publisher"));
books.Add(new Book("The Title2","Publisher"));
books.Add(new Book("A Book Title3","Another one"));
books.Add(new Book("title","Third one"));
var subset = books.Where(book => book.Title.StartsWith('A') && book.Publisher == "Publisher").OrderBy(b => b.Title).ToList();
foreach(var b in subset) {
System.Console.WriteLine(b.ToString());
}
Format((string str) => {
System.Console.WriteLine("we received: "+str);
});
}
static void Format(Action<string> act) {
act("testing it");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment