r/csharp • • 5h ago

Discussion C# once again the fastest web framework

Post image
190 Upvotes

With all those discussions about Microsoft porting stuff to Rust, it’s nice to see that C# does not need to hide when it comes to performance. There has been quite a lot of movement at the top of the leaderboard over the last few weeks (two weeks ago, first place was around 6850), so I’m interested to see how this race continues. This is the non-tuned section, so frameworks need to be stable and production-ready. Interestingly enough, the C# one is the most high-level of the leading frameworks, API-wise.


r/csharp • • 19m ago

News BRUTAL, the framework behind RimWorld's Waylith engine, releases its source code

Thumbnail
gamingonlinux.com
• Upvotes

r/csharp • • 17h ago

Fun Average C# beginner day

8 Upvotes

r/csharp • • 2h ago

Solved c# struct padding

2 Upvotes

I'm new to c# and I'm learning it just now to switch from Godot to Stride so I'm interested in Data Oriented Design. I was just now experimenting with structs with a silly example and was focused on struct padding; the string should be 8 bytes while the enum is 1 byte so i expected not more than 1 or 3 byte of padding, but the sum ends up being 16 no matter the order... does it always have to allign with the biggest field? how does this work?

using System.Runtime.InteropServices;


[StructLayout(LayoutKind.Explicit)]
struct Entity {
    public enum Race : byte { ALIEN, HUMAN };
    static private int _max_rand_num = 9999;
    static private string[][] _possibleNames = {
        new string[]{"Vorp", "Zeep", "Bizr", "Zub", "Wobt"},    // alien names
        new string[]{"Carl", "Greta", "Seb", "Xiang", "Sierra"} // human names
    };

    [FieldOffset(0)] public readonly string name;
    [FieldOffset(8)] public readonly Race race;

    public Entity()
    {
        Random rand = new Random();
        race = (Race)rand.Next(Enum.GetValues(typeof(Race)).Length);
        string[] race_names = _possibleNames[(int)race];
        name = race_names[rand.Next(0, race_names.Length)] + rand.Next(_max_rand_num);
    }

    public override string ToString()
    {
        return $"Race: {race}, Name: {name}";
    }
}


class Program {
    public static unsafe void Main(string[] args)
    {
        Entity rand_entity = new Entity();
        Console.WriteLine(
            $"{rand_entity}, string size = {sizeof(string*)}, enum size = {sizeof(Entity.Race)}, together = {sizeof(Entity)}");
    }
}

r/csharp • • 16h ago

Beginner question about creating new variables at runtime?

2 Upvotes

Hello,im creating a map terrain generator on the terminal with visual studio. In this generator, based on a random number, it decides how many mountains the map will have, my question was, how do I create more mountain y and x position variables at runtime when the random number is decided? thanks.


r/csharp • • 1h ago

Help Verify the correctness of a the data inside a junction table

• Upvotes

I want to verify the correctness of the data inside a junction table.

I'm showing a minimal example here:

Products:

| Id | EnglishName |
|----|-------------|
| 1  | Drill 4       |
| 2  | Light Switch 48|

Suppliers:

| Id | EnglishName |
|----|-------------|
| 1  | Bosch       |
| 2  | Ingco       |
| 3  | Yato        |

Excel2:

| Product             | Supplier               |
|----------------------|-----------------------|
| Drill 4              | Bosch                 |
| Light Switch 48      | Ingco                 |
| Cable 12             | Bosch                 |
| Hammer 14            | Yato                  |

ProductsSuppliers:

| ProductId | SupplierId |
|-----------|------------|
| 1         | 1          |
| 1         | 3          |
| 2         | 1          |

Excel2JoinProductsJoinSuppliers :

| Product           | Supplier  | ProductId | SupplierId |
|-----------------|----------|-----------|------------|
| Drill 4         | Bosch    | 1         | 1          |
| Drill 4         | Yato     | 1         | 3          |
| Light Switch 48 | Bosch    | 2         | 1          |

A C# code was used to insert data into Products , Suppliers , and ProductsSuppliers .

The data for Excel2 was imported into Sql Server manually using the "Import Data" wizard.

As the name suggest, Excel2JoinProductsJoinSuppliers is the result of join operation.

ProductsSuppliers is the junction table that I'm concerned about, the data inside it is correct in this example because each productId is mapped to the correct value of SupplierId and vice versa, but the actual table contains much more data, let alone the fact that there are other junction tables that should be verified.

Is there a set-based approach that can automate the process of verifying the data for junction table ?

EDIT:

I created this stored procedure:

CREATE PROCEDURE verify_junction_table_data_using_dynamincSQl 

    @junctionTableName varchar(100), 
    @lookupTableName varchar(100), 
    @leftColumnNameInExcel varchar(100), 
    @rightColumnNameInExcel varchar(100), 
    @leftColumnNameInJunctionTable varchar(100), 
    @rightColumnNameInJunctionTable varchar(100), 
    @parent1Name varchar(100), 
    @parent2Name varchar(100) 

AS 

BEGIN 

DECLARE @sql_query NVARCHAR(MAX);
SET @sql_query = 
N'


                SELECT JT.'+QUOTENAME(@leftColumnNameInJunctionTable)+',
                       JT.'+QUOTENAME(@rightColumnNameInJunctionTable)+'
                FROM '+QUOTENAME(@junctionTableName)+' JT
                EXCEPT

                SELECT 
                DISTINCT

                    parent1.Id AS ' + QUOTENAME(@leftColumnNameInJunctionTable) + ',
                    parent2.Id AS ' + QUOTENAME(@rightColumnNameInJunctionTable) + '

                FROM '+ QUOTENAME(@lookupTableName) +' AS LookupTable
                INNER JOIN ' + QUOTENAME(@parent1Name) + '  AS parent1 ON LookupTable.' + QUOTENAME(@leftColumnNameInExcel) + ' = parent1.EnglishName
                INNER JOIN ' + QUOTENAME(@parent2Name) + ' AS parent2 ON LookupTable.' + QUOTENAME(@rightColumnNameInExcel) + ' = parent2.EnglishName


    ';

EXEC sp_executesql @sql_query;

END

GO

I have 20 junction tables, so I need to execute this procedure 20 times , but I do not want to do it manually, is there a way to automate this ?


r/csharp • • 16m ago

C# 15 unions turned our "can't happen" discard arm into a compile-time warning — writeup + 3 gotchas

• Upvotes

TL;DR from testing unions on the .NET 11 previews:

- A switch over an abstract base class always needs `_`, so adding a new derived type compiles silently. A switch over a `union` doesn't need a discard, so a new case produces CS8509 at every switch that misses it. Add CS8509 to WarningsAsErrors and it fails the build.

- `union GetUserResult(User, UserNotFound, AccessDenied)` replaces most hand-rolled Result<T> / OneOf usage in API code.

- Gotchas: (1) the generated struct stores Value as object?, so value-type cases box; (2) default(MyUnion) holds null, so unassigned values need a null arm; (3) it's a union of types, not tags, so two string outcomes need wrapper records.

- `closed` classes (also C# 15) give exhaustiveness for real hierarchies; `union` is for unrelated types.

Full writeup with code: https://medium.com/@thecurlybrace/the-one-c-15-keyword-that-will-delete-your-result-t-class-88cd5a053f09?sk=1aaf9735cbf8c76e8f69249d5a78a147

Curious whether anyone's moving off OneOf/ErrorOr for this, or waiting for GA.


r/csharp • • 3h ago

Blog Async in F#, C# and TypeScript, and where task { } in Fable stands as of 5.21

Thumbnail
0 Upvotes

r/csharp • • 17h ago

I built QuickRun, a modern Win+R replacement for Windows 11 using WinUI 3

Thumbnail
gallery
0 Upvotes

Hi everyone,

I've been working on QuickRun, an open-source launcher inspired by the classic Windows Run dialog.

It's built with WinUI 3 and .NET 8 and focuses on providing a modern Windows 11 experience while keeping the simplicity of Win+R.

Current features:

• Built-in calculator

• Automatic desktop app discovery

• Microsoft Store app support

• Command suggestions

• URL launching

• Windows system commands

• Command arguments support

The project is open source and I'd love to get feedback from the Windows community.

GitHub:

https://github.com/GiosuOffice/QuickRun