C
C#10mo ago
Shinigami

✅ What is the best way to store basic C#?

Hi, I'm testing my C# code and learning from it. For example deserializing and serializing JSON using system.text.json, I have the working code in program.cs. Now i want to store it in a folder preferably same vscode folder to review it in future and test some other code. What's the best way to do it?
29 Replies
Pobiega
Pobiega10mo ago
Normally you'd just make another project C# and .NET are project based, not file based (the way python or js are) And just having a bunch of .cs files lying about is * not very practical * hard to find what you are looking for * will cause problems for the compiler so I'd probably use something like obsidian or some other "note-taking" app to keep snippets in, if thats what you are after or, make a new project
Shinigami
Shinigami10mo ago
Exactly, I'm using obsedian currently to take notes But was looking for a solution to keep all the code in one project and use it to test in program.cs
Pobiega
Pobiega10mo ago
okay here is what I do
Pobiega
Pobiega10mo ago
Pobiega
Pobiega10mo ago
all those folders are just... folders. My program.cs looks like this
namespace TestingConsoleApp;

public class Program
{
public static void Main()
{
CianData.CianDataProgram.Run();
}
}
namespace TestingConsoleApp;

public class Program
{
public static void Main()
{
CianData.CianDataProgram.Run();
}
}
so to avoid making a new project, every small snippet I make in its own "Program" class in its own namespace, and I just call that method from my real program method I have ~200 of these "mini-programs" inside this project
Shinigami
Shinigami10mo ago
i tried to create a folder and a class in it
Shinigami
Shinigami10mo ago
inside the folder what's the structure? waitttt that's brilliant, I also want to create something like Learnings.JsonSerializer.Run(). how do i do it? do i like create a main method in Jsonserializer.cs and call it from the Main program.cs? lemme try that @Pobiega can you let me know what I should keep in JSonSerializer.cs class to call it from my program.cs file?
Pobiega
Pobiega10mo ago
Hm, well first, don't call it JsonSerializer that name will conflict with the System.Text.Json class by the same name 😄 What I would do is make it a bit nested
Shinigami
Shinigami10mo ago
Gotcha, will change it's name
Pobiega
Pobiega10mo ago
Learnings.JsonSerialization.Program.Run() the first two are namespaces, then a class, then the run method
Shinigami
Shinigami10mo ago
Alright, let me try that
Pobiega
Pobiega10mo ago
I have a few other important tips for you 🙂 open up the terminal in VS Code and write dotnet new editorconfig
Shinigami
Shinigami10mo ago
please go ahead alr
Pobiega
Pobiega10mo ago
this will create a default .NET editorconfig file that will help you stick to the default rules for naming and code style next, dont use namespace xxx { ... } anymore prefer namespace xxx; this is called "file scoped namespaces" and is the default
Shinigami
Shinigami10mo ago
it did create a editorconfig file
Pobiega
Pobiega10mo ago
great classes are always named with PascalCase meaning person should be Person
Shinigami
Shinigami10mo ago
so you mean not to keep the classes and program main method inside namespace {} but just create namespace xxx; and create classes etc??
Pobiega
Pobiega10mo ago
in fact, Im sure you have a C# code formatter installed with VS Code?
Shinigami
Shinigami10mo ago
point taken
Pobiega
Pobiega10mo ago
file-scoped namespaces apply to everything in that file so there is no difference between
namespace TestingConsoleApp.CtorChains;

public static class Program
{
public static void Run()
{
var person = new Person("Steve", new DateOnly(2000, 01, 01));
Console.WriteLine(person);
}
}
namespace TestingConsoleApp.CtorChains;

public static class Program
{
public static void Run()
{
var person = new Person("Steve", new DateOnly(2000, 01, 01));
Console.WriteLine(person);
}
}
and
namespace TestingConsoleApp.CtorChains
{
public static class Program
{
public static void Run()
{
var person = new Person("Steve", new DateOnly(2000, 01, 01));
Console.WriteLine(person);
}
}
}
namespace TestingConsoleApp.CtorChains
{
public static class Program
{
public static void Run()
{
var person = new Person("Steve", new DateOnly(2000, 01, 01));
Console.WriteLine(person);
}
}
}
except that the top one is more readable, since you have one less level of indentation
Shinigami
Shinigami10mo ago
i see, gotcha! this is good will stick to namespace xxx; one what formatter do you use? i have prettier but i think thats only for html and stuff
Pobiega
Pobiega10mo ago
Well, I don't use VSCode 😛 Rider is my preferred IDE, with VS being my second. VS Code is a distant third but if I had to use VSCode, I'd use the C# Devkit lets see if we have a tag for it... $vscode
MODiX
MODiX10mo ago
Follow the instructions here on getting started with DevKit for C# in VSCode: https://code.visualstudio.com/docs/csharp/get-started
Get started with C# and .NET in Visual Studio Code
Getting Started with C# and .NET Development in Visual Studio Code
Pobiega
Pobiega10mo ago
there you go I'd highly recommend using regular VS if you can thou
Shinigami
Shinigami10mo ago
gotcha I have VS installed
Pobiega
Pobiega10mo ago
there ya go
Shinigami
Shinigami10mo ago
i can use it too, it's like when i do simple tests like creating arrays or something i learn on the go when i'd want to test i feel using vs easier but if i had to use VS then basically i'd create console app and just like what you did create multiple folder for each concept and execute it from main program.cs?
Pobiega
Pobiega10mo ago
that wouldnt change based on IDE/editor if you dont want to make new projects, thats what you need to do. and even then, you're limited to one project type (usually console) if you want to experiment with ASP.NET Core or WPF, that must be a new project
Shinigami
Shinigami10mo ago
right right, I just want one single project for everything C# concepts Thanks mate! learnt a lot Yep, it's working fine! just tested it.
Want results from more Discord servers?
Add your server
More Posts