i like chatgpt
i like chatgpt
CC#
Created by i like chatgpt on 2/15/2024 in #help
✅ Where does BenchmarkDotNet save my files?
I can run the following successfully but I cannot find where the files A.txt and B.txt are saved. Where are they saved?
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

BenchmarkRunner.Run<MyBenchmark>();


[MemoryDiagnoser]
public class MyBenchmark
{
Guid Id = Guid.NewGuid();

StreamWriter writerA = null!;
StreamWriter writerB = null!;

[GlobalSetup]
public void GlobalSetup()
{
writerA = new("A.txt");
writerB = new("B.txt");
}

[GlobalCleanup]
public void GlobalCleanup()
{
writerA?.Dispose();
writerB?.Dispose();
}

[Benchmark]
public async Task A() => await writerA.WriteLineAsync(Id.ToString());

[Benchmark]
public async Task B() => await writerB.WriteLineAsync(Id.ToString());
}
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

BenchmarkRunner.Run<MyBenchmark>();


[MemoryDiagnoser]
public class MyBenchmark
{
Guid Id = Guid.NewGuid();

StreamWriter writerA = null!;
StreamWriter writerB = null!;

[GlobalSetup]
public void GlobalSetup()
{
writerA = new("A.txt");
writerB = new("B.txt");
}

[GlobalCleanup]
public void GlobalCleanup()
{
writerA?.Dispose();
writerB?.Dispose();
}

[Benchmark]
public async Task A() => await writerA.WriteLineAsync(Id.ToString());

[Benchmark]
public async Task B() => await writerB.WriteLineAsync(Id.ToString());
}
8 replies
CC#
Created by i like chatgpt on 2/15/2024 in #help
✅ Why does Console.WindowWidth produce Invalid Handle when the app is invoked from Process?
I have two console application projects: HelloWorld and Invoker. HelloWorld
try
{
Console.Out.WriteLine(new string('=', Console.WindowWidth));
}
catch (Exception e)
{
Console.Error.WriteLine($"HelloWorld's error: {e.Message}");
}
try
{
Console.Out.WriteLine(new string('=', Console.WindowWidth));
}
catch (Exception e)
{
Console.Error.WriteLine($"HelloWorld's error: {e.Message}");
}
It works fine when executed directly. Invoker
ProcessStartInfo info = new()
{
FileName = "helloworld.exe",
RedirectStandardOutput = true,
RedirectStandardError = true,
};

using var process = new Process { StartInfo = info };
process.Start();

Task? outputTask = null;

if (!info.UseShellExecute && info.RedirectStandardError)
{
var errorWriter = process.StandardError;
Console.WriteLine($"Error: {errorWriter.ReadToEnd()}");
}

if(outputTask is not null) await outputTask;
await process.WaitForExitAsync();
ProcessStartInfo info = new()
{
FileName = "helloworld.exe",
RedirectStandardOutput = true,
RedirectStandardError = true,
};

using var process = new Process { StartInfo = info };
process.Start();

Task? outputTask = null;

if (!info.UseShellExecute && info.RedirectStandardError)
{
var errorWriter = process.StandardError;
Console.WriteLine($"Error: {errorWriter.ReadToEnd()}");
}

if(outputTask is not null) await outputTask;
await process.WaitForExitAsync();
It produces
Error: HelloWord's error: The handle is invalid.
What is wrong?
3 replies
CC#
Created by i like chatgpt on 2/14/2024 in #help
✅ Does the value of CreateNoWindow matter when UseShellExectute=true?
ProcessStartInfo info = new()
{
FileName = @"cmd",
Arguments = @"actions.bat",
CreateNoWindow = true, // either true or false it still creates a window
UseShellExecute = true
};
using var process = new Process { StartInfo = info };
process.Start();
ProcessStartInfo info = new()
{
FileName = @"cmd",
Arguments = @"actions.bat",
CreateNoWindow = true, // either true or false it still creates a window
UseShellExecute = true
};
using var process = new Process { StartInfo = info };
process.Start();
3 replies
CC#
Created by i like chatgpt on 2/6/2024 in #help
✅ Regex pattern to match unique words each with 3 unique characters
Given a multiline input text:
abc
aaaa
bcde
acd
abc
abc
xyz
abc
aaaa
bcde
acd
abc
abc
xyz
I am looking for a regex pattern that matches unique words each consists of exactly 3 unique characters. The expected output:
acd
xyz
acd
xyz
Edit: For new visitors, this question can actually be solved with negative lookbehind and negative lookahead. If you are in hurry, you can jump to https://discord.com/channels/143867839282020352/1204500978741673994/1204519081663926304 and return again to this message https://discord.com/channels/143867839282020352/1204500978741673994/1204500978741673994 in the future. I may update with the solution. 🙂
53 replies
CC#
Created by i like chatgpt on 2/5/2024 in #help
✅ Why is an explicit cast needed even though the implicit cast operator is available?
No explicit cast is needed.
var (x, y) = GetPoint();
static (float re, float im) GetPoint() => (1, 0);
var (x, y) = GetPoint();
static (float re, float im) GetPoint() => (1, 0);
But why does the following need explicit cast even though an implicit conversion operator has been implemented?
//var (x, y) = new Complex(1, 0); <== Error!

var (x, y) = ((float, float))new Complex(1, 0);

class Complex(float re, float im)
{
public float Re { get; set; } = re;
public float Im { get; set; } = im;
public static implicit operator (float re, float im)(Complex c) => (c.Re, c.Im);
}
//var (x, y) = new Complex(1, 0); <== Error!

var (x, y) = ((float, float))new Complex(1, 0);

class Complex(float re, float im)
{
public float Re { get; set; } = re;
public float Im { get; set; } = im;
public static implicit operator (float re, float im)(Complex c) => (c.Re, c.Im);
}
7 replies
CC#
Created by i like chatgpt on 2/5/2024 in #help
✅ Can my Equals(IEnumerable<T>? x, IEnumerable<T>? y) be made much simpler?
You may have any tricks to simplify the following.
public bool Equals(IEnumerable<T>? x, IEnumerable<T>? y)
{
if (x is null) return y is null;
if (y is null) return false;
return x.SequenceEqual(y);
}
public bool Equals(IEnumerable<T>? x, IEnumerable<T>? y)
{
if (x is null) return y is null;
if (y is null) return false;
return x.SequenceEqual(y);
}
6 replies
CC#
Created by i like chatgpt on 2/4/2024 in #help
✅ How to group by tautology with LINQ?
IEnumerable<Row> rows =
[
new Row(null, "_or_", null, null),
new Row(null, "_or_", true, true),
new Row(null, "_or_", false, null),
new Row(true, "_or_", true, true),
new Row(true, "_or_", false, true),
new Row(false, "_or_", false, false),

new Row(null, "or", null, null),
new Row(null, "or", true, true),
new Row(null, "or", false, null),
new Row(true, "or", true, true),
new Row(true, "or", false, true),
new Row(false, "or", false, false),

new Row(null, "xor", null, null),
new Row(null, "xor", true, null),
new Row(null, "xor", false, null),
new Row(true, "xor", true, false),
new Row(true, "xor", false, true),
new Row(false, "xor", false, false),
new Row(null, "xor_", null, null),
new Row(null, "xor_", true, null),
new Row(null, "xor_", false, null),
new Row(true, "xor_", true, false),
new Row(true, "xor_", false, true),
new Row(false, "xor_", false, false)
];
IEnumerable<Row> rows =
[
new Row(null, "_or_", null, null),
new Row(null, "_or_", true, true),
new Row(null, "_or_", false, null),
new Row(true, "_or_", true, true),
new Row(true, "_or_", false, true),
new Row(false, "_or_", false, false),

new Row(null, "or", null, null),
new Row(null, "or", true, true),
new Row(null, "or", false, null),
new Row(true, "or", true, true),
new Row(true, "or", false, true),
new Row(false, "or", false, false),

new Row(null, "xor", null, null),
new Row(null, "xor", true, null),
new Row(null, "xor", false, null),
new Row(true, "xor", true, false),
new Row(true, "xor", false, true),
new Row(false, "xor", false, false),
new Row(null, "xor_", null, null),
new Row(null, "xor_", true, null),
new Row(null, "xor_", false, null),
new Row(true, "xor_", true, false),
new Row(true, "xor_", false, true),
new Row(false, "xor_", false, false)
];
22 replies
CC#
Created by i like chatgpt on 2/4/2024 in #help
Can these ternary expression with expensive method calls be simplified without outline variables ...
I do NOT want to - call expensive methods more than once. - make "outline" temporary variables. Outline variables are ones declared outside ternary expressions. - leak variables with letters.
var out1 = ExpensiveMethod1(x, y) is bool __ ? __.ToString() : "null";
var out2 = ExpensiveMethod2(x, y) is bool ___ ? ___.ToString() : "null";
var out3 = ExpensiveMethod3(x, y) is bool ____ ? ____.ToString() : "null";
var out1 = ExpensiveMethod1(x, y) is bool __ ? __.ToString() : "null";
var out2 = ExpensiveMethod2(x, y) is bool ___ ? ___.ToString() : "null";
var out3 = ExpensiveMethod3(x, y) is bool ____ ? ____.ToString() : "null";
where bool? ExpensiveMethod*<T,U>(T t, U u){}. Can these expression be simplified even further?
10 replies
CC#
Created by i like chatgpt on 1/30/2024 in #help
When do we need to use static test method?
Consider the following test class.
public class MyTest
{
[Fact]
public static void Test_1() => Assert.True(true);
[Fact]
public void Test_2() => Assert.True(true);
}
public class MyTest
{
[Fact]
public static void Test_1() => Assert.True(true);
[Fact]
public void Test_2() => Assert.True(true);
}
Results:
Test Run Successful.
Total tests: 2
Passed: 2
Total time: 1.2190 Seconds
Test Run Successful.
Total tests: 2
Passed: 2
Total time: 1.2190 Seconds
Thus both static and instance test methods are invoked. From this results, I am wondering when we really need static test methods. I am not 100% sure with my opinion. Please correct me if I am wrong. Use static test methods to explicitly express our intent that the test methods don't want to use injected stuffs such as ITestOutputHelper because injected stuffs cannot be static. The chained constraints that prevent static methods from participating in DI: - Static methods can only access static members (other static methods, static fields, static properties, etc). - Static constructor must be parameterless so it cannot be injected. - Instance constructor cannot initialize static members.
2 replies
CC#
Created by i like chatgpt on 1/27/2024 in #help
What is the proper names for the following mutating methods?
What is the most proper names to replace the name of Mutator_A and Mutator_B? The names - must be descriptive so we know what they do only by reading their names - must not be too long
static void Mutator_A(ref File file)
{
file = new File { Id = Random.Shared.Next(), Name = Path.GetRandomFileName() };
}
static void Mutator_B(File file)
{
file.Id = Random.Shared.Next();
file.Name = Path.GetRandomFileName();
}
class File
{
public int Id;
public string Name = null!;
}
static void Mutator_A(ref File file)
{
file = new File { Id = Random.Shared.Next(), Name = Path.GetRandomFileName() };
}
static void Mutator_B(File file)
{
file.Id = Random.Shared.Next();
file.Name = Path.GetRandomFileName();
}
class File
{
public int Id;
public string Name = null!;
}
30 replies