Faker
Explore posts from servers✅ Is there a difference when we use { get; init; } vs { get; } ?
Hello guys, if I omit the
init
keyword here, would it make a difference? From what I've understood with or without the init
keyword, we would still be able to "modify" the object during instantiation, no?19 replies
✅ What is a primary constructor in C#
Hello guys, was just reading a bit about primary constructors. My question is, primary constructors is just a "fancy" way to write less code instead of declaring a whole constructor? Or are there any reasons why we would use primary constructors?
25 replies
✅ Is it possible to override ToString method for Anonymous type in C#? (Not using overriden one)
Hello guys, just wondering if it was possible to use another implementation of the
ToString
method for anonymous type.
I was just using LINQ to perform the following:
When I use a foreach and output each lesson, I have the overriden format of the ToString method.
I manage to change part of it using the following:
But just wanted to know if it's actually possible to change the ToString format again6 replies
✅ Problem updating rider
Hello guys, I tried to update to the latest version of rider but the thing is I didn't have enough disc space and so the update was cancelled. Now that I made enough space, when I try to update it again, it seems that nothing is happening, I had the "update" button but when it finish updating, the IDE remains as it is and it seems that when I click on certain buttons, like settings or plugins, nothing happens. Is there any solution to tackle this issue please... I even try to invalidate caches but nothing happens
36 replies
✅ How does explicit loading differs from lazy loading when using EF Core
Hello guys, I was reading about the methods used to load data in EF Core. I came across eager loading, explicit loading and lazy loading. I understood that eager loading is loading data directly with the query, while lazy loading is loading the data as we go, like in a for-each loop. But I didn't understand the explicit loading can someone explain please. If explicit loading is loading data when we need, isn't it the same thing as lazy loading?
35 replies
✅ Dispose method to "dispose" file resource when using LogTo method
Hello guys, I'm trying to implement some simple logging in my DbContext. I want to write the logs in a file; when done, I need to dispose/close the file. I read that we can't use the
using
keyword because this would mean that our file stream will be closed and we won't be able to open it again (I thing, can someone please confirm please) and so we need to use the Dispose
method.
Here is what is written in the docs, but I have the warning CA1816. I didn't understand what exactly I need to do, I can just add this line:
GC.SuppressFinalize(this);
?19 replies
✅ StreamWriter not writing to file
Hello guys, I changed my file properties to "Copy if newer" (I didn't understand why we need to do that, would really appreciate if someone can explain) .... I was able to read a file but when it comes to write to it, it seems that nothing is being written and no exception is being thrown (I added the .close method).
64 replies
✅ How does CultureInfo works in C#?
Hello guys, it's been quite a few instances now that I came across CultureInfo. I encounted it when comparing strings and when comparing dates. What I understood about it is that, if we don't specify anything, it uses the locale system of our machine. So if our machine is set to
en-us
and we try to compare something with de-DE
, their might be issues here? I know the "ß (Eszett)" stands for "ss" I think... so, what kind of issues might arise?
If we write something like this for eg:
str1 == str2 will return false in this case (If we don't change the CultureInfo thing)?
Now there is also the CultureInfo when we use dates. This is where I'm the most confused. When we use methods like TryParse
with DateOnly, it may happen that the overload contains the IFormatProvider interface, so basically these are "settings" to change the culture specific.
For string I did understand what it does (but please someone confirm if the above statements are correct please.), for date, it's a bit ambiguous though. So say my locale system has culture en-us
, basically, this mean that my date format is specific to the us culture. How will that affect my TryParse
or TryParseExact
?11 replies
✅ DateOnly.TryParse Vs DateOnly.TryParseExact use cases
Hello guys, I was reading a bit about
DateOnly
data type. I didn't understand the difference between DateOnly.TryParse
and DateOnly.TryParseExact
. Normally, when we use the parse method, the string given is parsed according to our system culture? How does TryParse and TryParseExact differs?8 replies
✅ What is the syntax for generics in methods signature
Hello guys, consider the following:
TraverseBreadthFirst<T>(T, Func<T, IEnumerable<T>>)
Declaration:
public static IEnumerable<T> TraverseBreadthFirst<T>(T root, Func<T, IEnumerable<T>> childrenSelector)
Can someone explain what T refers to please, is it the return type or the type that the argument can take? I'm always confuse by that... what if we can have multiple generic parameters? How would we represent them?
Is there a syntax to follow? for example in the Func<T, IEnumerable<T>>
delegate type, I know that T
is the argument and IEnumerable<T>>
is the return type20 replies
✅ Delegate vs lambda function/expression
Hello guys, I was just reading a bit about delegates and lambda expression. I thought that lambda expression and delegates were the same thing. For example, when I see something like:
Func<int,int>
.... is it a delegate? When such syntax appears in a method like in LINQ, there, what do we expect? Normally, we wrote something like s => s.DoSomething()
I'm a bit confused, what makes each one different from the other and when to recognise them please19 replies
✅ When to use interfaces and why
Hello guys, sorry to disturb you all; I understood how to implement interfaces, thinking of it like a kind of contract that we can implement. The thing is, say I need to perform some
CRUD
operations on a particular entity, say Student
entity, I would create a class for the student CRUD operations and create methods like adding new students, deleting existing student etc... My question is, in such cases, is it a good choice to just define an interface that list the method to be used for the CRUD
operations? If so, can someone explain why please43 replies
✅ Logic to choose different context from database in code
Hello guys, I have set up a database that has tables Instructor and Student. The thing is, both Student and Instructor have same properties and I need to perform CRUD Operations. This mean, the logic of the code remain the same, only the context will change each time.
My question is:
1. Do I implement an if statement logic to check whether we are interacting with instructor or student, then depending on that use appropriate context
OR
2. I declare 2 different class, one for StudentOperations and another for InstructorOperations?
But while doing so, don't we repeat the codes?
3. Can an interface be helpful here? I was thinking of implementing an interface to define the CRUD methods, but what's the point here?
8 replies
✅ Backing Field and Primary Constructors
Hello guys, I was reading a bit about backing fields and primary constructors. Can someone explain when is a backing field created and when does a primary constructor argument persist please.
For example, consider the following code:
For the first node class, I understood that t is not captured because we are assigning it directly to Data and we won't really use t later on and so it can be discarded. Data property also has a backing field.
On the other hand, in the second node class, we don't have a backing field but t is captured.
I don't understand why t is captured here and why is a backing field not generated, when is a backing field generated and when is the argument of a primary constructor captured please (just to clarify, a backing field is just a field that stores data, right?).
If we don't have a backing field, this mean, we don't have a private field for something? It's as if the property doesn't exist ?
24 replies
✅ Looping through collections with key value pairs
Hello guys, consider the following code:
I'm a bit confused about how grouping is used in the loops. The thing is I first tried to compare it with how we loop for a dictionary but in a dictionary, we would use
grouping.Value
in the inner loop but here, we used grouping
in both loops. What's happening here please, how does grouping know which one is an int or which one is the collection.17 replies
✅ Static keyword
Hello guys, I have a small question. Here, notice we use the
static
keyword with the array Country and City
. My question is, static keyword can only be used in the class where Country and City exist? That is, say we have class City and Country, in both classes we would have the declaration of an array of their respective type with static and readonly keyword? We can't use static keyword for a variable in our Program.cs for example?9 replies
✅ Use of Func as part of function definition
Hello guys, I came across this function definition where we use
Funct<>
as part of the function definition. I'm a bit lost here, what are we doing? I know that Func<>
is a delegate where we can store function references but here we aren't doing that. Oh, or may be that's because we are returning a function reference?17 replies
✅ Can we create "modules" that can be imported in C#?
Hello guys, I wanted to create a specific script where there are methods that can be imported. In order to do that, it's mandatory to create a new class? I mean since we are creating a new class, it will be mandatory to instantiate it or use the class name itself if it's static... it's not possible to just import a specific method ?
3 replies