Faker
Faker
Explore posts from servers
CC#
Created by Faker on 3/31/2025 in #help
Resource to learn making GUI in C#
Hello guys, is there any recommended resource to start making GUI from scratch in C# please
37 replies
CC#
Created by Faker on 3/26/2025 in #help
✅ Is there a difference when we use { get; init; } vs { get; } ?
C#
public record Person
{
public required string FirstName { get; init; }
public required string LastName { get; init; }
};
C#
public record Person
{
public required string FirstName { get; init; }
public required string LastName { get; init; }
};
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
CC#
Created by Faker on 3/26/2025 in #help
✅ 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
CC#
Created by Faker on 3/25/2025 in #help
✅ 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:
C#
var lessons = context.Lessons.Select(lessons => new
{
StudentFirstName = lessons.Student.FirstName,
StudentLastName = lessons.Student.LastName,
StudentEmail = lessons.Student.Email,
InstructorFirstName = lessons.Instructor.FirstName,
InstructorLastName = lessons.Instructor.LastName,
InstructorEmail = lessons.Instructor.Email,
CarTransmission = lessons.Car.Transmission,
LessonDate = lessons.Date
});
C#
var lessons = context.Lessons.Select(lessons => new
{
StudentFirstName = lessons.Student.FirstName,
StudentLastName = lessons.Student.LastName,
StudentEmail = lessons.Student.Email,
InstructorFirstName = lessons.Instructor.FirstName,
InstructorLastName = lessons.Instructor.LastName,
InstructorEmail = lessons.Instructor.Email,
CarTransmission = lessons.Car.Transmission,
LessonDate = lessons.Date
});
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:
C#
Console.WriteLine(lesson.ToString().Replace("{","").Replace("}","").TrimStart());
C#
Console.WriteLine(lesson.ToString().Replace("{","").Replace("}","").TrimStart());
But just wanted to know if it's actually possible to change the ToString format again
6 replies
CC#
Created by Faker on 3/24/2025 in #help
✅ 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
CC#
Created by Faker on 3/23/2025 in #help
✅ 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
CC#
Created by Faker on 3/23/2025 in #help
✅ 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.
C#
public override void Dispose()
{
base.Dispose();
_logStream.Dispose();
}
C#
public override void Dispose()
{
base.Dispose();
_logStream.Dispose();
}
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
CC#
Created by Faker on 3/23/2025 in #help
✅ 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).
C#
// How to read and write to file in C#
string filePath = Path.GetFullPath("sample.txt");

try
{
var sr = new StreamReader(filePath);
var line = sr.ReadLine();
Console.WriteLine(line);
sr.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

try
{
using (var sw = new StreamWriter(filePath, true, Encoding.Unicode))
{
sw.WriteLine("Just a new line");
sw.WriteLine("Just a new line 2");
}

}
catch (Exception e)
{
Console.WriteLine($"Processing failed: {e.Message}");
}
finally
{
Console.WriteLine("Finally block executed");
}
C#
// How to read and write to file in C#
string filePath = Path.GetFullPath("sample.txt");

try
{
var sr = new StreamReader(filePath);
var line = sr.ReadLine();
Console.WriteLine(line);
sr.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

try
{
using (var sw = new StreamWriter(filePath, true, Encoding.Unicode))
{
sw.WriteLine("Just a new line");
sw.WriteLine("Just a new line 2");
}

}
catch (Exception e)
{
Console.WriteLine($"Processing failed: {e.Message}");
}
finally
{
Console.WriteLine("Finally block executed");
}
64 replies
CC#
Created by Faker on 3/22/2025 in #help
✅ 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:
C#
var str1 = "Grasse";
var str2 = "Graße"
if (str1 == str2) ...
C#
var str1 = "Grasse";
var str2 = "Graße"
if (str1 == str2) ...
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
CC#
Created by Faker on 3/21/2025 in #help
✅ 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
CC#
Created by Faker on 3/19/2025 in #help
✅ 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 type
20 replies
CC#
Created by Faker on 3/16/2025 in #help
✅ 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 please
19 replies
CC#
Created by Faker on 3/15/2025 in #help
✅ 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 please
43 replies
CC#
Created by Faker on 3/15/2025 in #help
✅ 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
CC#
Created by Faker on 3/13/2025 in #help
✅ 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:
C#
private class Node(T t)
{
// No capture in this form. Data has a backing field, and you assign t to it.
public T Data { get; set; } = t;
}

private class Node(T t)
{
// Capture in this form. Data has no backing field, and t is captured as class state
public T Data => t;
}
C#
private class Node(T t)
{
// No capture in this form. Data has a backing field, and you assign t to it.
public T Data { get; set; } = t;
}

private class Node(T t)
{
// Capture in this form. Data has no backing field, and t is captured as class state
public T Data => t;
}
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
CC#
Created by Faker on 3/13/2025 in #help
✅ Looping through collections with key value pairs
Hello guys, consider the following code:
C#
// percentileQuery is an IEnumerable<IGrouping<int, Country>>
var percentileQuery =
from country in countries
let percentile = (int)country.Population / 1_000
group country by percentile into countryGroup
where countryGroup.Key >= 20
orderby countryGroup.Key
select countryGroup;

// grouping is an IGrouping<int, Country>
foreach (var grouping in percentileQuery)
{
Console.WriteLine(grouping.Key);
foreach (var country in grouping)
{
Console.WriteLine(country.Name + ":" + country.Population);
}
}
C#
// percentileQuery is an IEnumerable<IGrouping<int, Country>>
var percentileQuery =
from country in countries
let percentile = (int)country.Population / 1_000
group country by percentile into countryGroup
where countryGroup.Key >= 20
orderby countryGroup.Key
select countryGroup;

// grouping is an IGrouping<int, Country>
foreach (var grouping in percentileQuery)
{
Console.WriteLine(grouping.Key);
foreach (var country in grouping)
{
Console.WriteLine(country.Name + ":" + country.Population);
}
}
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
CC#
Created by Faker on 3/13/2025 in #help
✅ Static keyword
C#
static readonly City[] cities = [
new City("Tokyo", 37_833_000),
new City("Delhi", 30_290_000),
new City("Shanghai", 27_110_000),
new City("São Paulo", 22_043_000),
new City("Mumbai", 20_412_000),
new City("Beijing", 20_384_000),
new City("Cairo", 18_772_000),
new City("Dhaka", 17_598_000),
new City("Osaka", 19_281_000),
new City("New York-Newark", 18_604_000),
new City("Karachi", 16_094_000),
new City("Chongqing", 15_872_000),
new City("Istanbul", 15_029_000),
new City("Buenos Aires", 15_024_000),
new City("Kolkata", 14_850_000),
new City("Lagos", 14_368_000),
new City("Kinshasa", 14_342_000),
new City("Manila", 13_923_000),
new City("Rio de Janeiro", 13_374_000),
new City("Tianjin", 13_215_000)
];

static readonly Country[] countries = [
new Country ("Vatican City", 0.44, 526, [new City("Vatican City", 826)]),
new Country ("Monaco", 2.02, 38_000, [new City("Monte Carlo", 38_000)]),
new Country ("Nauru", 21, 10_900, [new City("Yaren", 1_100)]),
new Country ("Tuvalu", 26, 11_600, [new City("Funafuti", 6_200)]),
new Country ("San Marino", 61, 33_900, [new City("San Marino", 4_500)]),
new Country ("Liechtenstein", 160, 38_000, [new City("Vaduz", 5_200)]),
new Country ("Marshall Islands", 181, 58_000, [new City("Majuro", 28_000)]),
new Country ("Saint Kitts & Nevis", 261, 53_000, [new City("Basseterre", 13_000)])
];
C#
static readonly City[] cities = [
new City("Tokyo", 37_833_000),
new City("Delhi", 30_290_000),
new City("Shanghai", 27_110_000),
new City("São Paulo", 22_043_000),
new City("Mumbai", 20_412_000),
new City("Beijing", 20_384_000),
new City("Cairo", 18_772_000),
new City("Dhaka", 17_598_000),
new City("Osaka", 19_281_000),
new City("New York-Newark", 18_604_000),
new City("Karachi", 16_094_000),
new City("Chongqing", 15_872_000),
new City("Istanbul", 15_029_000),
new City("Buenos Aires", 15_024_000),
new City("Kolkata", 14_850_000),
new City("Lagos", 14_368_000),
new City("Kinshasa", 14_342_000),
new City("Manila", 13_923_000),
new City("Rio de Janeiro", 13_374_000),
new City("Tianjin", 13_215_000)
];

static readonly Country[] countries = [
new Country ("Vatican City", 0.44, 526, [new City("Vatican City", 826)]),
new Country ("Monaco", 2.02, 38_000, [new City("Monte Carlo", 38_000)]),
new Country ("Nauru", 21, 10_900, [new City("Yaren", 1_100)]),
new Country ("Tuvalu", 26, 11_600, [new City("Funafuti", 6_200)]),
new Country ("San Marino", 61, 33_900, [new City("San Marino", 4_500)]),
new Country ("Liechtenstein", 160, 38_000, [new City("Vaduz", 5_200)]),
new Country ("Marshall Islands", 181, 58_000, [new City("Majuro", 28_000)]),
new Country ("Saint Kitts & Nevis", 261, 53_000, [new City("Basseterre", 13_000)])
];
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
CC#
Created by Faker on 3/12/2025 in #help
✅ Nested from clause in query syntax
C#
var largeCitiesList = (
from country in countries
from city in country.Cities
where city.Population > 10000
select city
).ToList();

// or split the expression
IEnumerable<City> largeCitiesQuery =
from country in countries
from city in country.Cities
where city.Population > 10000
select city;
var largeCitiesList2 = largeCitiesQuery.ToList();
C#
var largeCitiesList = (
from country in countries
from city in country.Cities
where city.Population > 10000
select city
).ToList();

// or split the expression
IEnumerable<City> largeCitiesQuery =
from country in countries
from city in country.Cities
where city.Population > 10000
select city;
var largeCitiesList2 = largeCitiesQuery.ToList();
Hello guys, can someone explain the use of the nested from clause in the code above please. Why do we use nested from? Couldn't we just use the dot notation, like country.Cities? Why would it matter here?
17 replies
CC#
Created by Faker on 3/12/2025 in #help
✅ Use of Func as part of function definition
C#
static Func<int, int> GetMultiplier(int factor)
{
return (num) => num * factor;
}
C#
static Func<int, int> GetMultiplier(int factor)
{
return (num) => num * factor;
}
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
CC#
Created by Faker on 3/11/2025 in #help
✅ 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