Skip to content

Commit 7e4b589

Browse files
committed
Latest batch of linqpad c# doodads.
1 parent 0db634a commit 7e4b589

8 files changed

+230
-0
lines changed

BoxedStringEqualityBug.linq

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<Query Kind="Statements" />
2+
3+
string original = "abcedf";
4+
5+
// literal
6+
var a = original;
7+
a.Dump("a");
8+
var b = original;
9+
b.Dump("b");
10+
Console.Write($"{a} == {b}? : ");
11+
Console.WriteLine(a == b);
12+
13+
14+
// boxed only
15+
object boxeda = a;
16+
boxeda.Dump("boxeda");
17+
object boxedb = b;
18+
boxedb.Dump("boxedb");
19+
Console.Write($"{boxeda} == {boxedb}? : ");
20+
Console.WriteLine(boxeda == boxedb);
21+
22+
// boxed and constructed
23+
object boxedAndFormata = string.Format(a);
24+
boxedAndFormata.Dump("boxedAndFormata");
25+
object boxedAndFormatb = string.Format(b);
26+
boxedAndFormatb.Dump("boxedAndFormatb");
27+
28+
// ==
29+
Console.Write($"{boxedAndFormata} == {boxedAndFormatb}? : ");
30+
Console.WriteLine(boxedAndFormata == boxedAndFormatb);
31+
32+
// !=
33+
Console.Write($"{boxedAndFormata} != {boxedAndFormatb}? : ");
34+
Console.WriteLine(boxedAndFormata != boxedAndFormatb);
35+
36+
a.GetHashCode().Dump("a hashcode");
37+
b.GetHashCode().Dump("b hashcode");
38+
boxeda.GetHashCode().Dump("boxeda hashcode");
39+
boxedb.GetHashCode().Dump("boxedb hashcode");
40+
boxedAndFormata.GetHashCode().Dump("boxeda hashcode");
41+
boxedAndFormatb.GetHashCode().Dump("boxedb hashcode");
42+

CheckForSetInCollection.linq

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
var OnboardOutputLocations = new List<OutputLocation> {
6+
new OutputLocation(1, true),
7+
new OutputLocation(2, true),
8+
new OutputLocation(3, false),
9+
new OutputLocation(4, true),
10+
new OutputLocation(5, false),
11+
new OutputLocation(6, false),
12+
new OutputLocation(7, false),
13+
};
14+
var requiredPositions = new[] {1,2,3,4};
15+
16+
var oneToFourAvailable = false;
17+
oneToFourAvailable = requiredPositions.All(pos => OnboardOutputLocations.Exists(o => o.Position == pos && o.IsAvailable));
18+
oneToFourAvailable.Dump();
19+
}
20+
21+
// You can define other methods, fields, classes and namespaces here
22+
public class OutputLocation
23+
{
24+
public OutputLocation(int pos, bool avail)
25+
{
26+
Position = pos;
27+
IsAvailable = avail;
28+
}
29+
public int Position { get; set; }
30+
public bool IsAvailable {get; set; }
31+
}

IocSandbox.linq

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<Query Kind="Program">
2+
<NuGetReference>Prism.Core</NuGetReference>
3+
<NuGetReference>Prism.DryIoc</NuGetReference>
4+
<NuGetReference>Prism.Wpf</NuGetReference>
5+
<Namespace>Prism.Ioc</Namespace>
6+
<Namespace>Prism.DryIoc</Namespace>
7+
<Namespace>System.Windows</Namespace>
8+
<Namespace>DryIoc</Namespace>
9+
</Query>
10+
11+
void Main()
12+
{
13+
var myApp = new App();
14+
var serviceA = App.AppContainer.Resolve<IServiceA>();
15+
serviceA.SayHello().Dump("Service A Method");
16+
17+
if (serviceA is IServiceB serviceB)
18+
{
19+
serviceB.SayGoodbye().Dump("Service B Method");
20+
}
21+
}
22+
23+
// You can define other methods, fields, classes and namespaces here
24+
25+
public class App: PrismApplication
26+
{
27+
public static IContainer AppContainer { get; set; }
28+
29+
public App() {
30+
OnStartup(null);
31+
}
32+
33+
protected override void OnStartup(StartupEventArgs args)
34+
{
35+
base.OnStartup(args);
36+
}
37+
38+
protected override void RegisterTypes(IContainerRegistry containerRegistry)
39+
{
40+
containerRegistry.AddMyNewModulesServices();
41+
42+
AppContainer = containerRegistry.GetContainer();
43+
}
44+
45+
protected override Window CreateShell() { return new Window(); }
46+
}
47+
48+
public static class MyNewModuleContainerExtensions
49+
{
50+
public static IContainerRegistry AddMyNewModulesServices(this IContainerRegistry registry)
51+
{
52+
registry.Register<IServiceA, ServiceAbc>();
53+
registry.Register<IServiceB, ServiceAbc>();
54+
registry.Register<IServiceC, ServiceAbc>();
55+
56+
return registry;
57+
}
58+
}
59+
60+
public interface IServiceA { string SayHello(); }
61+
public interface IServiceB { string SayGoodbye(); }
62+
public interface IServiceC { }
63+
64+
public class ServiceAbc : IServiceA, IServiceB, IServiceC
65+
{
66+
public ServiceAbc() {}
67+
public string SayHello() { return "Hello World"; }
68+
public string SayGoodbye() { return "Goodbye!"; }
69+
}

JsonSandbox.linq

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<Query Kind="Program">
2+
<NuGetReference>Newtonsoft.Json</NuGetReference>
3+
<Namespace>Newtonsoft.Json</Namespace>
4+
<Namespace>Newtonsoft.Json.Linq</Namespace>
5+
<RemoveNamespace>System.Linq</RemoveNamespace>
6+
<RemoveNamespace>System.Linq.Expressions</RemoveNamespace>
7+
</Query>
8+
9+
void Main()
10+
{
11+
var s = new SensorEvent {
12+
sensor = "waste-cap-off-sensor",
13+
timestamp = DateTime.Now.Ticks,
14+
value = true
15+
};
16+
17+
var jsn = JsonConvert.SerializeObject(s);
18+
var tkn = JObject.Parse(jsn);
19+
var isSensor = tkn.SelectToken("sensor");
20+
(isSensor != null).Dump("IsSensor");
21+
jsn.Dump("json");
22+
}
23+
24+
// You can define other methods, fields, classes and namespaces here
25+
public class SensorEvent
26+
{
27+
public string sensor { get; set; }
28+
public long timestamp { get; set; }
29+
public bool value { get; set; }
30+
}

JulianToGregorian.linq

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<Query Kind="Expression" />
2+

LoopThreeChoices.linq

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Query Kind="Statements" />
2+
3+
var flag = true;
4+
var choices = new[] { "one", "two", "three" };
5+
for (var i=0; i < 10; i++)
6+
{
7+
choices[i % 3].Dump("choice");
8+
flag = !flag;
9+
flag.Dump("flag");
10+
}

MatchNumberInString.linq

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Query Kind="Statements" />
2+
3+
const string marker = "minutes";
4+
var pattern = new Regex($"([0-9]+)\\s(?:{marker})");
5+
var match = pattern.Match("over incubation by 8 minutes");
6+
if (!match.Success || match.Groups.Count != 2)
7+
{
8+
throw new InvalidOperationException("Unexpected description for over incubation. Unable to extract number of minutes.");
9+
}
10+
11+
var minutes = match.Groups[1].Value;
12+
minutes.Dump();
13+
14+
15+
match.Dump();
16+
match.Groups.Dump();

TaskUnobservedExceptionExample.linq

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<Query Kind="Program">
2+
<Namespace>System.Threading.Tasks</Namespace>
3+
</Query>
4+
5+
6+
class Program
7+
{
8+
static void ThrowEx() { throw null; }
9+
static void Main(string[] args)
10+
{
11+
TaskScheduler.UnobservedTaskException += new EventHandler<UnobservedTaskExceptionEventArgs>(TaskScheduler_UnobservedTaskException);
12+
13+
Task t3 = Task.Run(() =>
14+
{
15+
//try
16+
//{
17+
throw new Exception("t3 throw an exception");
18+
//}
19+
//catch {}
20+
});
21+
22+
Thread.Sleep(5000);
23+
}
24+
static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
25+
{
26+
Console.WriteLine("Error");
27+
Console.WriteLine(e.Exception.Message);
28+
e.SetObserved();
29+
}
30+
}

0 commit comments

Comments
 (0)