C Sharp: Difference between revisions
No edit summary |
No edit summary |
||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
==Usage== | |||
===Regular Expressions=== | |||
Regex<br> | |||
[https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex?view=netframework-4.8 Reference] | |||
<syntaxhighlight lang="csharp"> | |||
using System; | |||
using System.Text.RegularExpressions; | |||
public class Test | |||
{ | |||
public static void Main () | |||
{ | |||
// Define a regular expression for repeated words. | |||
Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b"); | |||
// Define a test string. | |||
string text = "The the quick brown fox fox jumps over the lazy dog dog."; | |||
// Find match. | |||
Match match = rx.Match(text); | |||
// Iterature through captures | |||
foreach (Capture c in match.captures) { | |||
Console.WriteLine("Captured {c.Value}"); | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
==Multithreading== | ==Multithreading== | ||
Line 15: | Line 44: | ||
ManualResetEvent finishedHandle = new ManualResetEvent(false); | ManualResetEvent finishedHandle = new ManualResetEvent(false); | ||
for (int i = 0; i < numberOfTasks; i++) { | for (int i = 0; i < numberOfTasks; i++) { | ||
// Note that you cannot reuse i here. | |||
// You can use this trick to put it in a closure | |||
// Or you can make a closure manually with another anonymous function | |||
// i.e. (a => { return _=>{ \\ Do stuff here })(a) | |||
int j = i; | |||
ThreadPool.QueueUserWorkItem(_ => { | ThreadPool.QueueUserWorkItem(_ => { | ||
try { | try { | ||
// Do something time consuming or resource intensive. | // Do something time consuming or resource intensive. | ||
// Do not use i here. You can use j instead. | |||
} catch (System.Exception e) { | } catch (System.Exception e) { | ||
// Print Stack Trace | // Print Stack Trace | ||
Line 34: | Line 69: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==Memory Management== | |||
C# has value types and reference types. Reference types are heap allocated similar to Java. Value types are allocated either on the stack or within objects on the heap, the same as primitives in Java or structs in C/C++. | |||
Reference types are created with classes. Value types are created with structs. | |||
==Platform Invoke== | |||
[https://docs.microsoft.com/en-us/dotnet/standard/native-interop/pinvoke Reference]<br> | |||
Platform Invoke (or P/Invoke) allows you to call and use C and C++ libraries from within your C# or other .NET programs. | |||
[[Category:Programming languages]] |
Latest revision as of 22:31, 15 February 2025
Usage
Regular Expressions
Regex
Reference
using System;
using System.Text.RegularExpressions;
public class Test
{
public static void Main ()
{
// Define a regular expression for repeated words.
Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b");
// Define a test string. <br />
string text = "The the quick brown fox fox jumps over the lazy dog dog.";
<br />
// Find match.
Match match = rx.Match(text);
// Iterature through captures
foreach (Capture c in match.captures) {
Console.WriteLine("Captured {c.Value}");
}
}
}
Multithreading
Theadpool
C# has a convenient TheadPool class in the System.Threading namespace.
class TestClass {
static void Main(string[] args) {
int numberOfTasks = 10;
int tasksRemaining = 10;
// This is similar to a mutex.
ManualResetEvent finishedHandle = new ManualResetEvent(false);
for (int i = 0; i < numberOfTasks; i++) {
// Note that you cannot reuse i here.
// You can use this trick to put it in a closure
// Or you can make a closure manually with another anonymous function
// i.e. (a => { return _=>{ \\ Do stuff here })(a)
int j = i;
ThreadPool.QueueUserWorkItem(_ => {
try {
// Do something time consuming or resource intensive.
// Do not use i here. You can use j instead.
} catch (System.Exception e) {
// Print Stack Trace
} finally {
if (Interlocked.Decrement(ref tasksRemaining) == 0) {
finishedHandle.Set();
}
}
});
}
// Blocking wait.
finishedHandle.WaitOne();
}
}
Memory Management
C# has value types and reference types. Reference types are heap allocated similar to Java. Value types are allocated either on the stack or within objects on the heap, the same as primitives in Java or structs in C/C++.
Reference types are created with classes. Value types are created with structs.
Platform Invoke
Reference
Platform Invoke (or P/Invoke) allows you to call and use C and C++ libraries from within your C# or other .NET programs.