1
13
2
7
submitted 4 days ago* (last edited 4 days ago) by jupiter@programming.dev to c/csharp@programming.dev

I made another oopsie, I mean, Open Sourcie. :3

Remember Haskell's newtype? Or F# type abbreviations?

Well, newtype is a package that lets you use similar semantics in #CSharp for a large number of types, including the ability to add semantics and extension-like methods to your own derived types.

MIT-Licensed, go wild!

https://github.com/outfox/newtype

https://www.nuget.org/packages/newtype

3
3
4
6
5
-2
6
3
7
6
8
3
9
2
Changing Immutable Collections (codeblog.jonskeet.uk)
10
4
11
1
12
3
Deep C# - The Console (www.i-programmer.info)
13
3
14
3
15
2
16
12
C# Is TIOBE Language of 2025 (www.i-programmer.info)
17
3
18
7
19
2
20
4
21
2
submitted 1 month ago* (last edited 1 month ago) by elBoberido@programming.dev to c/csharp@programming.dev

geteilt von: https://programming.dev/post/42846946

Hi everyone,

we, the iceoryx community, just released iceoryx2 v0.8, an ultra-low latency inter-process communication middleware in Rust, with C, C++, Python and with this release, C# bindings.

If you are into robotics, embedded real-time systems (especially safety-critical), autonomous vehicles or just want to hack around, iceoryx2 is built with you in mind.

Check out our release announcement for more details: https://ekxide.io/blog/iceoryx2-0.8-release

And the link to the project: https://github.com/eclipse-iceoryx/iceoryx2

22
-4
23
2

Hello o/

I was experimenting with writing a language implementation so bump into this new thing called "recursive descent parser" at first it seemed complex but as I programmed it everything magically worked. I will attach the code tell me if I am wrong somewhere.

namespace ContextFreeGrammarDemo
{
    static class Parser
    {
        static string code = "aaaabbbb";
        static int cursor = 0;

        public static void Parse()
        {
            if (cursor >= code.Length)
                return;
            char currentChar = code[cursor];

            if (currentChar == 'a')
            {
                Console.WriteLine("a");
                cursor++;
                Parse();
                if (cursor < code.Length && code[cursor] == 'b')
                {
                    Console.WriteLine("b");
                    cursor++;
                    Parse();
                }
                else
                {
                    Console.WriteLine("Oopsie");
                    Environment.Exit(1);
                }
            }
        }
    }
    class Program
    {
        public static void Main(string[] args)
        {
            Parser.Parse();
        }
    }
}
24
3
Aman Ghodawala's Website (sites.google.com)

This is my personal site where I will upload c sharp content, I hope you will like it :)

25
5

Platform Invoke (P/Invoke)

P/Invoke is a technology that allows you to call c/c++ function or function from a dll library from your managed code. It's divided into two namespace System and System.Runtime.InteropServices. you only need these namespaces to call external c/c++ function.

why do we need to call c/c++ function inside c#

  • It's more efficient than writing your own in c#
  • decreases code repetition

Windows Example

Here is an example of how you can use P/Invoke to call function from user32.dll.

using System;
using System.Runtime.InteropServices;

public partial class Program
{
    // Import user32.dll (containing the function we need) and define
    // the method corresponding to the native function.
    [LibraryImport("user32.dll", StringMarshalling = StringMarshalling.Utf16, SetLastError = true)]
    private static partial int MessageBoxW(IntPtr hWnd, string lpText, string lpCaption, uint uType);

    public static void Main(string[] args)
    {
        // Invoke the function as a regular managed method.
        MessageBoxW(IntPtr.Zero, "Command-line message box", "Attention!", 0);
    }
}

what happening in the snippet:

  • we have imported System and System.Runtime.InteropServices which contains the necessary attributes to call a function in a dll file.
  • the library user32.dll will be loaded at runtime and it will complete the implementation of MessageBoxW function. ( for those of who don't know what the function do: it's just showing a prompt to the user). keep in mind that this example is windows specific, it will not work on Linux or MacOS.
  • you have probably noticed that we doing StringMarshalling. ( Marshalling is the process of transforming types when they need to cross between c# to native code (c/c++ or dll))
  • you must define the c# function with the same signature of the c counterpart.

MacOS Example

using System;
using System.Runtime.InteropServices;

namespace PInvokeSamples
{
    public static partial class Program
    {
        // Import the libSystem shared library and define the method
        // corresponding to the native function.
        [LibraryImport("libSystem.dylib")]
        private static partial int getpid();

        public static void Main(string[] args)
        {
            // Invoke the function and get the process ID.
            int pid = getpid();
            Console.WriteLine(pid);
        }
    }
}
  • In the example we are loading libSystem.dylib library and calling a getpid() function inside that library.

Linux example

using System;
using System.Runtime.InteropServices;

namespace PInvokeSamples
{
    public static partial class Program
    {
        // Import the libc shared library and define the method
        // corresponding to the native function.
        [LibraryImport("libc.so.6")]
        private static partial int getpid();

        public static void Main(string[] args)
        {
            // Invoke the function and get the process ID.
            int pid = getpid();
            Console.WriteLine(pid);
        }
    }
}

Invoking managed code from unmanaged code

c# allows us to pass a callback function to unmanaged code which will be called when a particular event gets triggered. The closes thing to a function pointer in managed code is a delegate. keep in mind that you have to create the delegate which has the same signature as expected in the dll function.

Here is an example:

using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    public static partial class Program
    {
        // Define a delegate that corresponds to the unmanaged function.
        private delegate bool EnumWC(IntPtr hwnd, IntPtr lParam);

        // Import user32.dll (containing the function we need) and define
        // the method corresponding to the native function.
        [LibraryImport("user32.dll")]
        private static partial int EnumWindows(EnumWC lpEnumFunc, IntPtr lParam);

        // Define the implementation of the delegate; here, we simply output the window handle.
        private static bool OutputWindow(IntPtr hwnd, IntPtr lParam)
        {
            Console.WriteLine(hwnd.ToInt64());
            return true;
        }

        public static void Main(string[] args)
        {
            // Invoke the method; note the delegate as a first parameter.
            EnumWindows(OutputWindow, IntPtr.Zero);
        }
    }
}

I hope you learned something new today!

view more: next ›

C Sharp

1766 readers
1 users here now

A community about the C# programming language

Getting started

Useful resources

IDEs and code editors

Tools

Rules

Related communities

founded 2 years ago
MODERATORS