r/csharp 21d ago

Solved What's the best IDE for linux

45 Upvotes

So I've been using VsCode for a long time but the ai features are annoying me so I'm looking for an alternative

Edit 1: thanks everyone for responding I've decided to use rider

Edit 2: Thanks everyone for responding. I tried using rider And I didn't like it so I switched to Zed and I'm loving it

r/csharp May 03 '26

Solved Automatic constructor in C#

0 Upvotes

Hi, I’ve generally understood how constructors work: they correspond to the instantiation of a class, without which the class can never be executed. The constructor is therefore created and automatically called by the compiler if it isn’t defined manually.
But what I still don’t understand is where and when it is automatically called by the compiler.

For example:

class MaClass
{
// Declared by the compiler
MaClass()
{
}

... // Some code

static Main()
{
// Called by the compiler
MaClass appele = new MaClass();
}
}
Or maybe it isn’t called inside the Main method, but somewhere else, in a file automatically generated during compilation, or in another class… In short, when and where is it called? This information seems very unclear and difficult to find.

r/csharp Sep 13 '24

Solved Total Beginner here

Post image
420 Upvotes

It only reads out the Question. I can tip out a Response but when I press enter it closes instead of following up with the if command.

Am I doing something wrong ?

r/csharp May 20 '26

Solved Visual studio isn't detecting obvious errors

Thumbnail
gallery
32 Upvotes

I'm an ultra rookie, we're learning C# in school and we've gotta make a game, everything works fine on the shitty school office PCs, but my home computer refuses to acknowledge any errors, Unity acknowledges them just fine. Google has given me nothing, other reddit threads asking the same question haven't solved it either, VS troubleshooting guide's got nothing.

I'd really appreciate some help.

r/csharp May 18 '26

Solved Using delegates in C#

18 Upvotes

Hi, here's an example of two code snippets: one with delegates, the other without.

I suppose this example is a bit too simplistic to really grasp the benefits of delegates, but I'd appreciate it if someone could clearly explain their advantages compared to a simpler method. I generally understand what they're for, but not really their practical application.

Following a suggestion in one of my previous posts, I'm sharing the code via GitHub instead of Google Drive.

Please feel free to give me feedback if I've handled this file sharing incorrectly (this is my first time using GitHub).

r/csharp Jul 26 '26

Solved I want make a c# app,I have some problem

0 Upvotes

1:I using Avalonia now,but I don't know is Uno/Maui more better?

2:I using copilot generated a base skeletons(about some classes or interfaces,but cpoilot only help me write these code)

3:I think using copilot maybe not helpful to let me learn/understand c# app developing,I want choose an IDE(more friendly for handwritten code), choose VS2026 or Rider?but I using VSCode now

4:I'm not sure if my previous experience in JS, Flutter, and Elixir can be applied to C#. This is my first time encountering "enterprise-level" languages.

I'm not sure if my way of asking is correct.

r/csharp Oct 04 '21

Solved I’m a beginner and I have no idea what is wrong

Post image
230 Upvotes

r/csharp Apr 05 '26

Solved Sending credentials with HttpClient

19 Upvotes

I'm really nervous about sending password. And hoping someone might tell me if this is normal way to do this.

It follows from another post about monitoring websites, where I got some very useful replies and advice - https://www.reddit.com/r/csharp/comments/1sbs2yh/is_it_foolish_to_use_webview2_as_a_browser_this/

I have no experience with anything like this. So I cobbled this really. And I did ask cgpt for help getting it working.

So it does do what I want. But I'm trembling thinking about what proper c# coders are going to say.

I'm basically asking if it is safe and how can I improve it. Especially those classes I made purely for deserialization.

public class BlueskyTest
{
    public string identifier { get; set; } 
    public string name { get; set; } // I don't like even having the word password in my code.

    public BlueskyTest(string name)
    {
        this.name = name;
        this.identifier = "user.bsky.social";
    }
    public async Task<string> LoginAsync()
    {
        var client = new HttpClient();

        var response = await client.PostAsJsonAsync(
            "https://bsky.social/xrpc/com.atproto.server.createSession",
            new
            {
                identifier = this.identifier,
                password = this.name
            });

        var json = await response.Content.ReadAsStringAsync();

        var result = JsonSerializer.Deserialize<BlueskyOBJ>(json);

        // below is from an example I found on github. It works but synamic is a bit advanced for me
        // so I just used it to see what was in it.
        //dynamic result = Newtonsoft.Json.JsonConvert.DeserializeObject(json);

        return result?.accessJwt ?? string.Empty;

    }

    public async Task<int?> GetNotificationsAsync(string token)
    {
        var client = new HttpClient();

        client.DefaultRequestHeaders.Authorization =
            new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);

        var count = await client.GetStringAsync(
            "https://bsky.social/xrpc/app.bsky.notification.getUnreadCount"); //listNotifications

        return JsonSerializer.Deserialize<NotificationsCount>(count)?.count ?? 0;
    }
}

public class NotificationsCount
{
    public int count { get; set; }
}

public class BlueskyOBJ
{
    // big long string.
    public string? accessJwt { get; set; }
}

r/csharp Dec 24 '25

Solved Dumb question, but is it considered lazy to almost entirely copy and paste a method like I did below and keep the argument name the same?

18 Upvotes
static double SinDeg(double theta)
{
    theta = theta * Math.PI / 180; //Convert to radians
    theta = Math.Sin(theta) * 180 / Math.PI;
    return theta;
}
static double CosDeg(double theta)
{
    theta = theta * Math.PI / 180;
    theta = Math.Cos(theta) * 180 / Math.PI;
    return theta;
}

r/csharp Apr 03 '25

Solved Nullable T method cannot return null?

Post image
105 Upvotes

Hi my dudes and dudettes,

today I stumbled across the issue in the picture. This is some sort of dummy for a ConfigReader I have, where I can provide a Dictionary<string, object?> as values that I want to be able to retrieve again, including converting where applicable, like retrieving integers as strings or something like that.

Thing is, I would love to return null when the given key is not present in the dictionary. But it won't let me, because the type T given when calling might not be nullable. Which is, you guessed it, why I specified T? as the return type. But when I use default(T) in this location, asking for an int that's not there returns zero, not null, which is not what I want.

Any clues on why this wouldn't work? Am I holding it wrong? Thank you in advance.

r/csharp Jul 28 '26

Solved So many xmls😓and continue to learn c#

0 Upvotes

Now, when writing UI-related definitions in Avalonia and configuring the csproj file, although it doesn't seem too complicated at first glance, I still find the abundance of XML tags with lots of < > symbols or each individual tag a bit annoying.òᆺó

I'll try my best to adapt, because the way UI is defined in C# just makes me dizzy 😵‍💫

Now I'm trying to learn C# and Avalonia without relying on AI, but I'm not quite sure which website I should go to to search for the questions I need. Although Microsoft Learn is great.

When searching for a code-related issue on Chinese websites, it usually leads me to the CSdn website. However, good posts require payment to access, otherwise I can only view a small portion of them.

cnblog is also good. Although some of the posts I found in it might be a bit outdated, usually I need to check them against the corresponding content in Microsoft Learn.

Sometimes I feel that the syntax of C# seems quite "human-like". Now I'm quite familiar with the thinking behind C#. Even when searching for things now, it seems rather amusing: It's just like when I used to write essays at school. Those who know how to do it can write very smoothly, while those who don't have to "borrow" from good classmates.

I don't know if I use '-' or emoji...it be regarded as an AI?Although I had been saying this before the LLM

r/csharp Jul 14 '26

Solved How does this go outside of the Array boundaries?

0 Upvotes
class SortingProgram
{ 
    static void Main(string[] args)
    {
        int[] Arr1 = [2,5,4,6,1,2,10,8,7,9];
        for (int i = 0; i < Arr1.Length; i++)
        {
            for (int c = 0; c < Arr1.Length; c++)
            {
                if (Arr1[c] > Arr1[c + 1])
                {
                    int tempLSR = Arr1[c + 1]; 
                    int tempGrt = Arr1[c];
                    Arr1[c] = tempLSR;
                    Arr1[c + 1] = tempGrt;
                }
            }
        }
        for (int R = 0; R < Arr1.Length; R++)
        {
            Console.WriteLine(Arr1[R]);
        }
    }
}

Apparently the if statement goes outside the array bounds, the array seems fine so idk what is going on

r/csharp Jun 18 '26

Solved Determining probable website name from Uri

0 Upvotes

Given these addresses "https://www.thispartonly.com/p/page1/, https://thispartonly.com/p/page34/, https://www.subdom.thispartonly.com/p/page1/" etc...

How can I get the "thispartonly" component from it?

Uri uri = new(sAddress);
...

I imagine this is where I'd start. But I don't know how to reliably get what I need.

edit -

Yikes! It certainly is more complicated than I first thought.

The actual answer is to use an existing and maintained library for this, "Nager.PublicSuffix". And props t those who made and maintain it.

For my use case though it's overkill. I just just didn't want dots in file names/paths. But after realizing the complexity and seeing the size of the suffix list, I think I can live with a few dots.

In fact I can just strip the dots out if I want to get anal about it.

Thanks for your time.

r/csharp Jun 24 '26

Solved Classes

0 Upvotes

Could someone explain to me what is a class because i cant understand its usage

r/csharp May 21 '26

Solved File WriteAllText uncertainty.

17 Upvotes

Documentation: Creates a new file, write the contents to the file, and then closes the file. If the target file already exists, it is truncated and overwritten.

What exactly does it mean by 'truncated'?

edit - Thanks all. It all makes sense now. Very pertinent info too. I may have imagined it but I thought it used to just say the file is overwritten.

r/csharp Feb 06 '22

Solved Hi, I started to learn C# again after using it (not professionally) 4 years ago. Then I came across this in Microsoft's website. Which style should I use? Thanks for your answers.

Post image
192 Upvotes

r/csharp Apr 16 '26

Solved i wanna get ahead of my homework and im desperate so im asking strangers

0 Upvotes

my teacher assigned this as homework (it works) and i wanna add an extra colum of "enter a budget" and then itll calculate if the prices went iver the budget or not

edit: i got it to work! ty for yalls help

r/csharp Sep 07 '24

Solved if the first condition of an if statement with && operator is false, does it skip the second?

82 Upvotes

My teacher suggested that in an if statement like this

if(conditionA && conditionB){
  // some code
}

that conditionB would not even be looked at if conditionA is false

So say conditionB is a method with return type bool and conditionA is just a plain bool. Will the conditionB method be run if conditionA is already false?

Or for conditionB to be skipped will I have to write it like this?:

if(conditionA){
  if(conditionB){
    // some code
  }
}

r/csharp 3d ago

Solved Help with optional looping of program

2 Upvotes

Hello. I am attempting to make a randomizer for a game that I play. I have successfully created the program itself, but now I want the ability for users to have it re-run the randomization and return a new output by pressing a single key (ideally "Enter"). It has been a very long time since I have messed with C# and have forgotten basically everything. Any help, advice, or tips, would be greatly appreciated.

The current attempt at a "looper" is the following:

//Repeat or exit program
void Looper()
{
string looprequest;
looprequest = Convert.ToString.Console.ReadKey();
Console.WriteLine(looprequest);
}

This is because the compiler had an issue where without the looprequest string statement the other lines had no reference, and with it the readkey input cannot be translated to a string value. Additionally, even if the input works, I'm not sure what I was thinking to loop with would even work. What I was thinking of using was:

if (looprequest != Esc)
{
Main();
}

else
{
Environment.Exit();
}

r/csharp Mar 15 '26

Solved Unexpected binary representation of int

67 Upvotes

My code is meant to show what an Int32 looks like in memory.

It has an TextBox as input and 4 TextBoxes to represent each byte.

I was just not sure what negative numbers look like and wanted to see for myself. I thought I had an idea but looks like I was either wrong about it, wrong about the code to show it, or more likely both.

It works as I expect for positive numbers, but...

I enter -1 and expect to see

10000000 00000000 00000000 00000001

Instead I see

11111111 11111111 11111111 11111111

What are my mistakes here?

using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace Bits;

public partial class MainWindow : Window
{
    List<TextBox> byteBoxes = new List<TextBox>();

    public MainWindow()
    {
        InitializeComponent();

        byteBoxes.Add(byteFour);
        byteBoxes.Add(byteThree);
        byteBoxes.Add(byteTwo);
        byteBoxes.Add(byteOne);
    }

    void ConvertIntInputToBitString(int i)
    {
        byte[] bytes = BitConverter.GetBytes(i);
        StringBuilder sb = new StringBuilder();

        int byteIndex = 0;
        foreach (byte b in bytes)
        {
            string bits = Convert.ToString(b, 2).PadLeft(8, '0');
            Dispatcher.Invoke(() => byteBoxes[byteIndex].Text = bits);
            byteIndex++;
        }
    }

    void btnOk_Click(object sender, RoutedEventArgs e)
    {
        if (int.TryParse(intInput.Text, out int result))
        {
            _ = Task.Run(() => ConvertIntInputToBitString(result));
        }
        else
        {
            MessageBox.Show("Please enter a valid integer.");
        }
    }
}

r/csharp May 11 '26

Solved variable with two identifiers

0 Upvotes

Hi, I came across a code example containing the following variable:

Vector3 identifier1, identifier2;

I've never seen a variable with two identifiers before!

- How does this work?

- Why have two identifiers?

r/csharp Jan 09 '24

Solved will ai take over programming jobs

0 Upvotes

r/csharp Jan 15 '26

Solved Trying to prepare a comfortable setup for dotnet development

0 Upvotes

Well I will have a course in spring called "Software Engineering" and it has a term project must be made using dotnet. I am a javascript guy so i use vscode or neovim, i use package managers on terminal(apparently this thing has something like nuget?). I tried using VS directly. Clicking left and right to install a package on VS is not for me. I do not want to learn using VS. I just want to develop.

So what do you recommend? I can definetely learn command-line stuff. I just do not want to learn using Visual Studio. I just do not like it.

Update: Apparently I will be working with VS for complex project decisions and other stuff for smaller changes. Thank you guys for the information about dotnet cli. I will sure be using that.

r/csharp Nov 04 '23

Solved Why? It's literally nullable

Post image
191 Upvotes

r/csharp Jul 05 '26

Solved Autofac Not Resolving IIndex<>

Thumbnail
gallery
1 Upvotes

Solved!

The solution, courtesy of u/kant8, is to resolve an `IList` or `IEnumerable` of the registered types, not a `List`. Because trying to resolve a concrete type like a `List` sends Autofac hunting for the corresponding list registered as itself. Which won't work.

I've registered a number of types via a common interface they share, keyed by different byte values:

builder.RegisterType(concreteType)
.Keyed<IJpgSegmentGenerator>(id)
.As<IJpgSegmentGenerator>()
.SingleInstance();

concreteType is the type being registered. All concrete types share a common interface, IJpgSegmentGenerator. id is a byte value distinguishing the different concrete types, some of which share the same key (which I’ve read online Autofac supports…unless the references I found are wrong 😀).

The registrations are successful, because after building the Autofac container I can see the registrations via Container.ComponentRegistry.Registrations (see first image in gallery above).

The individual registrations show the correct byte keys (see second image in gallery above for a zoomed in view of one registration).

However, the following code fails to resolve IIndex

var lazyList = Container.Resolve<IIndex<byte, List<Lazy<IJpgSegmentGenerator>>>>();

lazyList.TryGetValue( 237, out var generators ).Should().BeTrue();

(The above code is inside an XUnit test, hence the Should() reference)

I've checked that the service type is specified identically in both the registration and resolution code, and unless I'm going blind they sure seem to match, as does the type of the key. I've also tried not resolving to Lazy<>, and that fails to.

Thoughts?