steven preadly
steven preadly
Explore posts from servers
CC#
Created by steven preadly on 8/15/2024 in #help
✅ Use() Extension Overload
I'm working with the Use method in ASP.NET Core and have encountered two overloads. The first, taking HttpContext and RequestDelegate, is clear to me. However, I'm struggling to understand the second overload where the use lambda accepts HttpContext and Func<Task>. Some articles mention that this approach leads to additional allocations per request. Could someone elaborate on what these extra allocations are and how they impact performance?
7 replies
CC#
Created by steven preadly on 8/15/2024 in #help
✅ Factory-based middleware activation in asp.net core
this part of the docs says that Factory-based middleware is activated per request while when i register this middleware in app service container it is registered as a transient or scoped , this part confuse me is it registered twice https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/extensibility?view=aspnetcore-8.0
IMiddlewareFactory/IMiddleware is an extensibility point for middleware activation that offers the following benefits:

Activation per client request (injection of scoped services)
Strong typing of middleware
UseMiddleware extension methods check if a middleware's registered type implements IMiddleware. If it does, the IMiddlewareFactory instance registered in the container is used to resolve the IMiddleware implementation instead of using the convention-based middleware activation logic. The middleware is registered as a scoped or transient service in the app's service container.

IMiddleware is activated per client request (connection), so scoped services can be injected into the middleware's constructor.
IMiddlewareFactory/IMiddleware is an extensibility point for middleware activation that offers the following benefits:

Activation per client request (injection of scoped services)
Strong typing of middleware
UseMiddleware extension methods check if a middleware's registered type implements IMiddleware. If it does, the IMiddlewareFactory instance registered in the container is used to resolve the IMiddleware implementation instead of using the convention-based middleware activation logic. The middleware is registered as a scoped or transient service in the app's service container.

IMiddleware is activated per client request (connection), so scoped services can be injected into the middleware's constructor.
25 replies
CC#
Created by steven preadly on 8/10/2024 in #help
✅ ASP.NET Core: Confusion with builder.Services.AddTransient() and App Lifetime Management
I'm new to ASP.NET Core, and I'm encountering some confusion regarding service registration. In the code snippet below:
c#
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddTransient<RequestCultureMiddleware>();
var app = builder.Build();
c#
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddTransient<RequestCultureMiddleware>();
var app = builder.Build();
We see that builder.Services.AddTransient<RequestCultureMiddleware>() is used to register the RequestCultureMiddleware service with a transient lifetime. However, I understand that the WebApplication.CreateBuilder(args) method builds the application configuration, while service registration is performed at the application level using app. My question is: Why can't we register services like RequestCultureMiddleware directly with app.Services.AddTransient<RequestCultureMiddleware>() instead of using builder.Services? What's the underlying principle behind this separation?
13 replies
CC#
Created by steven preadly on 8/10/2024 in #help
✅ Extension Methods
this is an extension method that i have read in the docs , my question is when i use this extension method as below code in that case the this keyword bind the sentence string inside method as i did not pass any parameter correct?
c#
namespace StringExtnsion
{
public static class WordCountExtinsion
{
//this is used to bind the method to the main class
public static int WordCount(this String str)
{
string[] strings = str.Split(new char[] { ' ' , '?' });

return strings.Length;
}
}
}
c#
namespace StringExtnsion
{
public static class WordCountExtinsion
{
//this is used to bind the method to the main class
public static int WordCount(this String str)
{
string[] strings = str.Split(new char[] { ' ' , '?' });

return strings.Length;
}
}
}
c#
string? sentence = "My Name Is Mina?mina";

Console.WriteLine(sentence.WordCount());
c#
string? sentence = "My Name Is Mina?mina";

Console.WriteLine(sentence.WordCount());
8 replies
CC#
Created by steven preadly on 8/4/2024 in #help
why reading from a file in webforms application using StreamReader class dose not block the browser?
While making so in a windows form application the application got frozen tell the reading ends note :I am using Thread.Sleep()
11 replies
CC#
Created by steven preadly on 8/4/2024 in #help
✅ do using the lock object in the below code make seance?
think this i real program that has users will prevent the other users to use _ aqurie in case of readin or writting
c#
internal class Program
{



static int? _aquired;

static object _lock = new object();

static void Main(string[] args)
{
Thread T1 = new Thread(() => {
int aquire = default;

if(int.TryParse(Console.ReadLine(), out aquire))
{

lock (_lock)
{
_aquired = aquire;
}

}
});

T1.Start();

T1.Join();



Thread T2 = new Thread(() => {

lock (_lock)
{
if (_aquired != default)
{
for (int i = 0; i <= _aquired; i++)
{
Console.WriteLine("The Turn = {0}", i);
}

}
}


});

T2.Start();



}


}


}
c#
internal class Program
{



static int? _aquired;

static object _lock = new object();

static void Main(string[] args)
{
Thread T1 = new Thread(() => {
int aquire = default;

if(int.TryParse(Console.ReadLine(), out aquire))
{

lock (_lock)
{
_aquired = aquire;
}

}
});

T1.Start();

T1.Join();



Thread T2 = new Thread(() => {

lock (_lock)
{
if (_aquired != default)
{
for (int i = 0; i <= _aquired; i++)
{
Console.WriteLine("The Turn = {0}", i);
}

}
}


});

T2.Start();



}


}


}
38 replies
CC#
Created by steven preadly on 8/2/2024 in #help
✅ Thread and Threading
i have asked this question on the #help-0 an here is the link https://discord.com/channels/143867839282020352/169726586931773440/1269020486952816671 according to conversation with @TeBeCo i have changed the code to be like below by question is now T1.join() will block the main thread till it resolves , and then the T2 will block the main thread and the T1 Thread till it resolves is that correct
c#
static void Main(string[] args)
{
Console.WriteLine("Main Thread Start");

Thread T1 = new Thread(ThreadOneMethod);

T1.Start();

T1.Join();

Thread T2 = new Thread(ThreadTwoMethod);

T2.Start();

T2.Join();

Console.WriteLine("Thread Two Ends");

Console.WriteLine("Thread One Ends");

Console.WriteLine("Main Thread Ends");

}

public static void ThreadOneMethod()
{
Console.WriteLine("Thread One Starts");
}


public static void ThreadTwoMethod()
{
Console.WriteLine("Thread Two Starts");

}

public static void ThreadThreeMethod()
{
Console.WriteLine("Thread Three Starts");
}
c#
static void Main(string[] args)
{
Console.WriteLine("Main Thread Start");

Thread T1 = new Thread(ThreadOneMethod);

T1.Start();

T1.Join();

Thread T2 = new Thread(ThreadTwoMethod);

T2.Start();

T2.Join();

Console.WriteLine("Thread Two Ends");

Console.WriteLine("Thread One Ends");

Console.WriteLine("Main Thread Ends");

}

public static void ThreadOneMethod()
{
Console.WriteLine("Thread One Starts");
}


public static void ThreadTwoMethod()
{
Console.WriteLine("Thread Two Starts");

}

public static void ThreadThreeMethod()
{
Console.WriteLine("Thread Three Starts");
}
24 replies
CC#
Created by steven preadly on 7/13/2024 in #help
✅ positional parameters
dose this sentence You can use positional parameters in a primary constructor to create and instantiate a type with immutable properties. form docs means that the record create a primary constractor that contains the parameter , or it means that i can create a primary constractor inside the record type with the parameter , as i read the record creates by default a primary constructor and a proparites that is inilized using this constractor
31 replies
CC#
Created by steven preadly on 7/11/2024 in #help
why the Clist.Contains(customer) is not getting true in that case ?
why the Clist.Contains(customer) is not getting true while the new object that i created is already on the list dose that means that i have to override the Equals method in the customer class
c#

List<Customer> Clist = new List<Customer>(2)
{
new Customer() { Id = 1, Name = "mina" },
new Customer() { Id = 2, Name = "shaker" },
new Customer() { Id = 3, Name = "Kirollos" },
new Customer() { Id = 4, Name = "georget" }
};

Customer customer = new Customer() { Id = 1, Name = "mina" };


if (Clist.Contains(customer))
{
Console.WriteLine("Customer Exits");
}
else
{
Console.WriteLine("customer is not in the list");
}
c#

List<Customer> Clist = new List<Customer>(2)
{
new Customer() { Id = 1, Name = "mina" },
new Customer() { Id = 2, Name = "shaker" },
new Customer() { Id = 3, Name = "Kirollos" },
new Customer() { Id = 4, Name = "georget" }
};

Customer customer = new Customer() { Id = 1, Name = "mina" };


if (Clist.Contains(customer))
{
Console.WriteLine("Customer Exits");
}
else
{
Console.WriteLine("customer is not in the list");
}
i have tried to override the Equals() method in the customer class but, what was intersting is that it works
c#
public class Customer
{
public int Id { get; set; }
public string? Name { get; set; }

public override bool Equals(object? obj)
{
if (obj == null) return false;
if (obj.GetType() != typeof(Customer)) return false;
return (this.Name == ((Customer)obj).Name && this.Id == ((Customer)obj).Id);
}

public override int GetHashCode()
{
return this.Id.GetHashCode();
}
}
c#
public class Customer
{
public int Id { get; set; }
public string? Name { get; set; }

public override bool Equals(object? obj)
{
if (obj == null) return false;
if (obj.GetType() != typeof(Customer)) return false;
return (this.Name == ((Customer)obj).Name && this.Id == ((Customer)obj).Id);
}

public override int GetHashCode()
{
return this.Id.GetHashCode();
}
}
32 replies
CC#
Created by steven preadly on 7/9/2024 in #help
✅ I have been hired in a company that is still using c# with webforms asp.net
I have been hired in a company that is still using c# with webforms asp.net , they dont have the intension to migrate to aspCore i am one of the juniors in my team what should i do can any one give me opinion? do i have to study asp.net webforms fully because i have only the basics of it and them as a update for my self i study asp.net core ? or what should i do
21 replies
CC#
Created by steven preadly on 7/7/2024 in #help
✅ what will be the default value of the `params int[] value` if i did not pass it while calling M
what will be the default value of the params int[] value if i did not pass it while calling method , do this will be trated as an empty array of ints
c#

public static void Sum(int number , int number2,params int[] values)
{
Console.WriteLine(number);
Console.WriteLine(number2);
Console.WriteLine(values is int[]);
}
c#

public static void Sum(int number , int number2,params int[] values)
{
Console.WriteLine(number);
Console.WriteLine(number2);
Console.WriteLine(values is int[]);
}
22 replies
CC#
Created by steven preadly on 7/7/2024 in #help
i dont know why i am still got `CS8602 - Dereference of a possibly null reference.` in that indexer
here is an indexer i still got this warning in the setter i dont know why
c#
public string? this[int employee_id]
{
get => listEmployees?.FirstOrDefault(emp => emp.EmployeeId == employee_id)?.Name ?? "No Name Found";

set => listEmployees.FirstOrDefault(emp => emp.EmployeeId == employee_id).Name = value;
}
c#
public string? this[int employee_id]
{
get => listEmployees?.FirstOrDefault(emp => emp.EmployeeId == employee_id)?.Name ?? "No Name Found";

set => listEmployees.FirstOrDefault(emp => emp.EmployeeId == employee_id).Name = value;
}
34 replies
CC#
Created by steven preadly on 7/4/2024 in #help
✅ how can i improve this code ?
c#
c#
78 replies
KPCKevin Powell - Community
Created by steven preadly on 6/29/2024 in #front-end
what are the ways that i can follow to mange the height in card created using bootstrap
No description
18 replies
KPCKevin Powell - Community
Created by steven preadly on 6/20/2024 in #front-end
Why is the <div> with position-absolute and start-100 positioned outside the container?
No description
17 replies
CC#
Created by steven preadly on 5/28/2024 in #help
can I ask about your opinion for this description that prove that structs is value type
c#
/*
* in this example we have a structure and a class
* and those are done to prove the statment above
* that the struct is a value type while the class is
* refrence type
*/
public struct Point
{
public int x;
public int y;
}


class MyClass
{
public int x;
}

internal class Program
{
private static void Main(string[] args)
{
/*
* in here we used the struct we inilize it with a value x = 10
* and y = 20
*/
Point p1 = new Point { x = 10, y = 20 };

// then we write the value of x to the console
// it gets (10)
Console.WriteLine(p1.x);

//we used the ModifayStruct(Point P1) and we passed the P1 struct to it
// where this method modifayes the p1.x value to 12 other than 10
ModifayStruct(p1);

// the result when writting to the console is 10
// this is happiesn becuse the struct is a value type when we passed the struct to
// a method we are passing a copy of the struct not an adress to it in the memory
Console.WriteLine(p1.x);


/*
* in here we instatiated a mew Myclass object with x feild is 10
*/
MyClass @class = new MyClass { x = 10 };

// we used the ModifayClass(MyClass obj) and passed the above object to it
// where this method modifies x feild inside of it
ModifayClass(@class);

// the result is that x got modified that becuse when we passed a refrence varible to the
// method it got passed by refrence whicj means we are passng the address of that object
// to the method
Console.WriteLine(@class.x);

}

// modifays the point structs feild x to 12
public static void ModifayStruct(Point p1)
{
p1.x = 12;
}

// modifays the point class feild x to 12
public static void ModifayClass(MyClass obj)
{
obj.x = 12;
}
}
c#
/*
* in this example we have a structure and a class
* and those are done to prove the statment above
* that the struct is a value type while the class is
* refrence type
*/
public struct Point
{
public int x;
public int y;
}


class MyClass
{
public int x;
}

internal class Program
{
private static void Main(string[] args)
{
/*
* in here we used the struct we inilize it with a value x = 10
* and y = 20
*/
Point p1 = new Point { x = 10, y = 20 };

// then we write the value of x to the console
// it gets (10)
Console.WriteLine(p1.x);

//we used the ModifayStruct(Point P1) and we passed the P1 struct to it
// where this method modifayes the p1.x value to 12 other than 10
ModifayStruct(p1);

// the result when writting to the console is 10
// this is happiesn becuse the struct is a value type when we passed the struct to
// a method we are passing a copy of the struct not an adress to it in the memory
Console.WriteLine(p1.x);


/*
* in here we instatiated a mew Myclass object with x feild is 10
*/
MyClass @class = new MyClass { x = 10 };

// we used the ModifayClass(MyClass obj) and passed the above object to it
// where this method modifies x feild inside of it
ModifayClass(@class);

// the result is that x got modified that becuse when we passed a refrence varible to the
// method it got passed by refrence whicj means we are passng the address of that object
// to the method
Console.WriteLine(@class.x);

}

// modifays the point structs feild x to 12
public static void ModifayStruct(Point p1)
{
p1.x = 12;
}

// modifays the point class feild x to 12
public static void ModifayClass(MyClass obj)
{
obj.x = 12;
}
}
3 replies
CC#
Created by steven preadly on 5/28/2024 in #help
When should i use Private constractor with factory method?
here included example
c#
public class Student
{

private string name;
private Student(string name)
{
this.name = name;
}

public static Student CreateObject(string name)
{
return new Student(name);
}

public string Name
{
get { return name; }
}


}
c#
public class Student
{

private string name;
private Student(string name)
{
this.name = name;
}

public static Student CreateObject(string name)
{
return new Student(name);
}

public string Name
{
get { return name; }
}


}
12 replies
CC#
Created by steven preadly on 5/27/2024 in #help
Calculating Student Grade Book Average (GBA) in Percent - Encountering Zero Value Issue
I'm working on a student class with properties mark and totalMark. My goal is to define the totalMark property to compute the GBA as a percentage. However, every time I implement the calculation, the totalMark value consistently returns 0.
c#
public class Student
{

private int totalmarks;

public string? Name { get; set; }

public int Marks { get; set; }

public int TotalMarks
{
get
{
return (this.Marks / 250) * 100;
}
}

}

Student student = new Student();

student.Marks = 120;

int gba = student.TotalMarks;

Console.WriteLine(gba);
c#
public class Student
{

private int totalmarks;

public string? Name { get; set; }

public int Marks { get; set; }

public int TotalMarks
{
get
{
return (this.Marks / 250) * 100;
}
}

}

Student student = new Student();

student.Marks = 120;

int gba = student.TotalMarks;

Console.WriteLine(gba);
63 replies
CC#
Created by steven preadly on 5/26/2024 in #help
is the method hiding considered a type of polymorphisim?
is the method hiding considered a type of polymorphisim?
2 replies
CC#
Created by steven preadly on 5/24/2024 in #help
Polymorphism
dose the below sentence simply describes the meaning of Polymorphism
polymorphism is a Opp pillar that treats the objects created from derived class as a base class at runtime
polymorphism is a Opp pillar that treats the objects created from derived class as a base class at runtime
as an example in here the reference variable is pointing to the Employee base class while at run time the method is called on the PartTimeEmployee() type correct ?
c#
public class Employee
{
public string? firstName = "FN";
public string? lastName = "LN";

public virtual void PrintFullName()
{
Console.WriteLine(firstName + " " + lastName);
}
}

public class PartTimeEmployee: Employee
{
public override void PrintFullName()
{
Console.WriteLine(firstName + " " + lastName + " - PartTime");
}
}
Employee employee = new PartTimeEmployee();

employee.PrintFullName();
c#
public class Employee
{
public string? firstName = "FN";
public string? lastName = "LN";

public virtual void PrintFullName()
{
Console.WriteLine(firstName + " " + lastName);
}
}

public class PartTimeEmployee: Employee
{
public override void PrintFullName()
{
Console.WriteLine(firstName + " " + lastName + " - PartTime");
}
}
Employee employee = new PartTimeEmployee();

employee.PrintFullName();
and that is polymorphisim we have a base class method in one class(type) that can have many forms in drived class and the type that the method are called from is defined at runtime
2 replies