Skip to content

Instantly share code, notes, and snippets.

View ohlawdie's full-sized avatar
😟

Ohlawdie ohlawdie

😟
  • Michigan
View GitHub Profile
@ohlawdie
ohlawdie / filesystem.cs
Last active December 22, 2019 00:08
msdn filesystem
class FileSysInfo
{
static void Main()
{
// You can also use System.Environment.GetLogicalDrives to
// obtain names of all logical drives on the computer.
System.IO.DriveInfo di = new System.IO.DriveInfo(@"C:\");
Console.WriteLine(di.TotalFreeSpace);
Console.WriteLine(di.VolumeLabel);
@ohlawdie
ohlawdie / callback.cs
Last active February 11, 2020 22:53
Callback #delegate #callback
If you're used to function pointers in C, a delegate is basically a pair of pointers rolled into one:
A pointer to an object (optional)
A pointer to a method of that object
That means a single delegate passes all the information needed to locate a function in your program, whether it's a static method or associated with an object.
You define them like this in C#:
public delegate void FooCallbackType( int a, int b, int c );
@ohlawdie
ohlawdie / removeXMLwRegex
Last active December 23, 2019 19:20
#xml #regex
/.*?\n
Regex for removing xml comments
@ohlawdie
ohlawdie / TupleInCLR
Last active February 11, 2020 22:47
tuple #note
// With identity conversions you may have generic variance in mind, however, this is not possible due to
// Identity conversions are mostly important for tuples when it comes to constructed types. If you could easily convert from (int, int) to (int x, int y) but not from IEnumerable<(int, int)> to IEnumerable(<int x, int y>) you would be quite annoyed.
//Although you can ignore some preceding information and ignore what the CLR does with tuples, you'll be able to do more with tuples and better understand the behavior.
//Unlike anonymous types, in which unique sequence of property names within an assembly causes the compiler to generate a new type, typles don't require any extra types to be generated by the compiler. Instead, it uses a new set of types from the framework.
//Tuples in C# 7are implemented using the System.ValueTuple. A description of any type of is very much like the description of tuple types from earlier: it's a value type with public fields. You might expect the ValueTuple to be static - i
@ohlawdie
ohlawdie / Dynamism
Last active February 11, 2020 22:48
dynamic #note
Dynamic typing supports custom behavior via IDynamicMetaObjectProvider and the DynamicObject class
Dynamic typing is implemented with both compiler and framework features. The framework optimizes and caches for efficiency.
@ohlawdie
ohlawdie / WordDoc
Last active October 13, 2019 17:12
werd
Application app = new Application { Visible = true };
Document doc = app.Documents.Add*(;
Paragraph para = doc.Paragraphs.Add();
para.Range.Text = "Simple new code";
doc.SaveAs2<-->(FileName: "demo2.docx");
doc.Close();
app.Application.Quit();
@ohlawdie
ohlawdie / NontrailingNamedArgumentGenericInvoke.cs
Last active February 11, 2020 22:48
NamedGenericNonInvoke #weird
void M(int x, int y) { }
void M<T>(T y, int x) { }
void M2()
{
M(3, 4);
M(y: 3, x: 4); // Invokes M(int, int)
M(y: 3, 4); // Invokes M<T>(T, int)
}
@ohlawdie
ohlawdie / disposal.cs
Last active February 11, 2020 22:51
IDisposable
public class Example : IDisposable
{
private IntPtr buffer; // неуправляемый буфер памяти
private SafeHandle resource; // управляемый ресурс
public Example() // конструктор
{
this.buffer = ...
this.resource = ...
}
@ohlawdie
ohlawdie / ref return.cs
Last active February 11, 2020 22:47
ref
static void Main()
{
var store = new NumberStore();
WriteLine($"Исходная последовательность: {store.ToString()}");
ref var value = ref store.FindNumber(16);
value *= 2;
WriteLine($"Новая последовательность: {store.ToString()}");
//: 1 3 7 15 31 63 127 255 511 1023
//: 1 3 7 15 62 63 127 255 511 1023
@ohlawdie
ohlawdie / &.cs
Last active February 11, 2020 22:51
C# pointer #note
&array[0]
[DllImport("kernel32", SetLastError = true)]
static extern unsafe IntPtr CreateFile(
string FileName,
uint DesiredAccess,
uint ShareMode,
uint SecurityAttributes,
uint CreationDisposition,