Coder Baba
Open in Telegram
Everything about programming for beginners. 1 and only official telegram channel of CODERBABA India. Content: .NET Developer, Programming (ASP. NET, VB. NET, C#, SQL Server), & Projects follow me https://linktr.ee/coderbaba *Programming *Coding *Note
Show more2 319
Subscribers
-124 hours
-167 days
-2830 days
Posts Archive
2 319
āļø š„š²š½š¹š®š°š² š¶š³ ššš®šš²šŗš²š»š šš¶ššµ š”šš¹š¹ šš¼š»š±š¶šš¶š¼š»š®š¹ š¢š½š²šæš®šš¼šæ
2 319
āļø šØšš² šššæš¶š»š“.šš¾šš®š¹š š¶š»ššš²š®š± š¼š³ š§š¼šØš½š½š²šæ()/š§š¼šš¼šš²šæ() ššµš²š» š°š¼šŗš½š®šæš¶š»š“ šššæš¶š»š“š
š Using š§š¼šØš½š½š²šæ() and š§š¼šš¼šš²šæ() for case conversion in C# can impact performance due to memory allocation, string copying, and potential garbage collection, especially in situations involving large strings or frequent conversions.
š š¦ššæš¶š»š“.šš¾šš®š¹š is faster than ToUpper() or ToLower() due to direct character comparison, avoiding memory allocation, and reducing overhead for case-insensitive string comparison.
š„ To perform string comparison , it's better to use the built-in comparison methods like š¦ššæš¶š»š“.šš¾šš®š¹š with appropriate StringComparison options, which handle case-insensitivity and cultural considerations correctly while maintaining better performance and accuracy.
2 319
āļø šš²š°š¹š®šæš®šš¶š¼š» š£š®ššš²šæš»
ā
You use declaration and type patterns to check if the run-time type of an expression is compatible with a given type. With a š±š²š°š¹š®šæš®šš¶š¼š» š½š®ššš²šæš», you can also declare a new local variable. When a declaration pattern matches an expression, that variable is assigned a converted expression result.
š„ šš±šš®š»šš®š“š²š š¼š³ ššš¶š»š“ š±š²š°š¹š®šæš®šš¶š¼š» š½š®ššš²šæš»š:
ā¾ļø They can make your code more readable and maintainable.
ā¾ļø They can help you to avoid errors caused by type mismatches.
ā¾ļø They can be used to write more concise and expressive code.
š§šµš®š»šø šš¼š š³š¼šæ šæš²š®š±š¶š»š“ š
2 319
š”š# š§š¶š½
āļøšØšš² š»š®šŗš²š¼š³() šš¼ š°š¼š»šš²šæš š®š» š²š»ššŗ šš¼ š® šššæš¶š»š“
ā
The š§š¼š¦ššæš¶š»š“ method, inherited from the System.Object class, is used to convert a value into its string representation. It can be overridden in derived classes to provide a meaningful string representation of the object's current state.
ā
The š»š®šŗš²š¼š³ keyword, introduced in C# 6.0, is used to get the name of a variable, type, or member as a string at compile-time. It's useful to avoid magic strings in your code and to keep your code refactor-friendly.
š š»š®šŗš²š¼š³() is the preferred way to convert an enum to a string. This is because nameof() is evaluated at compile time and will inject a string literal that will never change, while ToString() is evaluated at runtime. This means that nameof() is more efficient and can help to improve the performance of your code.
š” Using nameof() with enums can lead to mismatched enum member names if the enum changes without recompiling referencing projects, as nameof() provides names at compile-time.
š§šµš®š»šø šš¼š š³š¼šæ šæš²š®š±š¶š»š“ š
2 319
āļøšØšš² š»š®šŗš²š¼š³() šš¼ š°š¼š»šš²šæš š®š» š²š»ššŗ šš¼ š® šššæš¶š»š“
2 319
š”š# š§š¶š½
āļøšØšš² šš¶š²š¹š± šæš²šššæš» šš¼ šŗš¶š»š¶šŗš¶šš² šŗš²šŗš¼šæš ššš®š“š²
ā
The šš¶š²š¹š± keyword is used in an iterator block to provide a value to the enumerator object or to signal the end of iteration. When used with return, it provides a value, and when used with break, it signals the end of iteration.
ā
The šš¶š²š¹š± keyword is often used for custom iteration over a collection. It's a powerful tool for writing more efficient code. It's worth noting that yield can only be used in the body of methods, operators, or accessors, and those have to return either IEnumerable, IEnumerable, IEnumerator, or IEnumerator.
š„ šš±šš®š»šš®š“š²š š¼š³ ššš¶š»š“ šš¶š²š¹š±:
ā¾ļøšš²š³š²šæšæš²š± š²š
š²š°ššš¶š¼š»: The code in an iterator block (where yield return is used) is not executed until the sequence is enumerated. This can lead to performance benefits because it allows elements to be generated on demand rather than all at once.
ā¾ļøš¦š¶šŗš½š¹š²šæ š°š¼š±š²: When creating a collection that needs to be iterated over, yield can simplify your code by abstracting away the need to create and manage your own collection.
ā¾ļøš š²šŗš¼šæš šš³š³š¶š°š¶š²š»š°š: When iterating over large collections or sequences, yield can be more memory efficient because it does not require the entire collection to be held in memory at once.
š§šµš®š»šø šš¼š š³š¼šæ šæš²š®š±š¶š»š“ š
2 319
āļøšØšš² šš¶š²š¹š± šæš²šššæš» šš¼ šŗš¶š»š¶šŗš¶šš² šŗš²šŗš¼šæš ššš®š“š²
2 319
āļø š£š®šæš®š¹š¹š²š¹ šš¼šæšš®š°šµ
š The regular š³š¼šæš²š®š°šµ loop is a sequential construct. It iterates over a collection or an enumerable in a single-threaded manner, processing each element one after the other. It's a good choice when the tasks performed inside the loop are relatively simple and quick to execute, and there's no need to parallelize them.
š The š£š®šæš®š¹š¹š²š¹.šš¼šæšš®š°šµ construct is part of the System.Threading.Tasks namespace and is designed for parallel execution of tasks across multiple threads. It divides the input collection into smaller partitions and processes them concurrently on separate threads.
ā
The performance of š³š¼šæš²š®š°šµ and š£š®šæš®š¹š¹š²š¹.šš¼šæšš®š°šµ depends on the specific scenario. In general, Parallel.ForEach can be faster than foreach if the collection is large and the work being done is well-suited for parallel execution. However, there are some cases where foreach may be faster than Parallel.ForEach.
š§šµš®š»šø šš¼š š³š¼šæ šæš²š®š±š¶š»š“ š
2 319
š”š# š§š¶š½
āļø š£š®šæš®š¹š¹š²š¹ šš¼šæšš®š°šµ
2 319
āļøš£šæš¼š½š²šæšš š£š®ššš²šæš» š š®šš°šµš¶š»š“ in C#
ā
It is allows you to test whether an expression's properties or fields match specific values or nested patterns. It's a concise and expressive way to perform conditional logic based on object structures.
š„ šš±šš®š»šš®š“š²š:
ā¾ļø šš¼š»š°š¶šš²š»š²šš: Can be used to create more concise and readable code.
ā¾ļø šš
š½šæš²ššš¶šš²š»š²šš: Can be used to express more complex conditions.
ā¾ļø š§šš½š² šš®š³š²šš: Ensures type correctness at compile time, reducing potential runtime errors.
š¤ Which one do you prefer?
š§šµš®š»šø šš¼š š³š¼šæ šæš²š®š±š¶š»š“ š
2 319
āļø š”šš¹š¹ š®šæš“ššŗš²š»š š°šµš²š°šøš š¶š» š# šš¬ š®š»š± .š”šš§ š²
ā
The ššæš“ššŗš²š»šš”šš¹š¹šš
š°š²š½šš¶š¼š».š§šµšæš¼ššš³š”šš¹š¹ method is a static method in the System namespace that throws an ššæš“ššŗš²š»šš”šš¹š¹šš
š°š²š½šš¶š¼š» if the specified object is null. It is a convenient way to check for null parameters in your code. It can help to prevent runtime errors and make the code more concise.
š„ šš±šš®š»šš®š“š²š š¼š³ ššæš“ššŗš²š»šš”šš¹š¹šš
š°š²š½šš¶š¼š».š§šµšæš¼ššš³š”šš¹š¹:
ā¾ļø It is easy to use. Just pass the object you want to check for null to the method, and it will throw an exception if the object is null.
ā¾ļø It is reduce code size and make it easy to read.
š¤ Which one do you prefer?
š§šµš®š»šø šš¼š š³š¼šæ šæš²š®š±š¶š»š“ š
2 319
š” The ššš¶šš°šµ š²š
š½šæš²ššš¶š¼š» was introduced in C# 8 as a more concise and expressive alternative to the traditional switch statement. It allows you to assign a value to a variable based on the value of an expression. In a switch expression, you use the => syntax to specify the value to assign if the expression matches a certain case. The _ is a discard symbol and is used as the "default" case.
ā
Both the ššš¶šš°šµ ššš®šš²šŗš²š»š and the ššš¶šš°šµ š²š
š½šæš²ššš¶š¼š» are used for similar purposes, the switch expression offers more concise syntax and greater flexibility for pattern matching and value assignment, making it a more powerful tool for modern C# development.
š¤ Which one do you prefer?
š§šµš®š»šø šš¼š š³š¼šæ šæš²š®š±š¶š»š“ š
2 319
āļøš¦šš¶šš°šµ šš
š½šæš²ššš¶š¼š» in C#
šÆ The ššš¶šš°šµ ššš®šš²šŗš²š»š has been part of C# since its early versions. It allows you to evaluate an expression against a series of case values and execute code blocks based on the matched case. Each case value must be a constant value that is known at compile-time. After a case block is executed, you usually need to include a break statement to exit the switch statement.
2 319
š”š# š§š¶š½
āļø šØšš² šæš²š°š¼šæš±š š³š¼šæ šš§š¢š
ā
š„š²š°š¼šæš±š are a feature introduced in š# šµ.š¬ that allows you to create simple, immutable data types. They are particularly useful for representing šš§š¢š (Data Transfer Objects) because they provide a concise syntax for defining classes that are primarily used to transfer data between layers of an application, such as between the business logic layer and the presentation layer.
ā
š„š²š°š¼šæš±š are best suited for simple data structures, and they are not meant to replace classes for all scenarios. For more complex types with behavior, you may still want to use regular classes or other features provided by C#.
š„ š„š²š°š¼šæš±š are an excellent choice for creating šš§š¢š due to their simplicity, immutability, and concise syntax, which helps in writing clean and maintainable code.
š§šµš®š»šø šš¼š š³š¼šæ šæš²š®š±š¶š»š“ š
2 319
AutoGenerate ID in aspdotnet C#
āāāāāāāāāāāāāāāāāāā
private void AutoGenerateID()
{
SqlConnection con=new SqlConnection("");
string qr="select 'EMP' + cast(max(cast(SUBSTRING(employeeid,4,5)as int))+1 as nvarchar)as nextkey from tblemployee";
SqlCommand cmd=new SqlCommand("qr",con);
if(con.State==ConnectionState.Closed)
{
con.Open();
}
SqlDataReader dr=new cmd.ExecuteReader();
while(dr.read())
{
if(dr[0].ToString()!="" || dr[0].ToString().trim()!=string.empty)
{
string id=dr[0].ToString();
textempid.text=id;
}
else
{
textempid.text="EMP1";
}
}
if(dr.HasRows)
{
dr.Close();
}
}
________________________________________________
Available now! Telegram Research 2025 ā the year's key insights 
