CSharp - Code Performance

本文探讨了C#中字符串(String)与可变字符串(StringBuilder)的区别,对比了两者在字符串拼接时的性能表现,并介绍了装箱与拆箱的概念及其对性能的影响。

/*

Author: Jiangong SUN

*/


1) String vs StringBuilder

System.String is immutable(unchangeable). 
System.StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed. 

StringBuilder is more performant. 

When you do string concatenation, like 

string str = str1 + str2 + str3;

The C# + concatenation operator builds a new string and causes reduced performance when it concatenates large amounts of text.

However, the .NET Framework includes a StringBuilder class that is optimized for string concatenation. It provides the same benefits as using a character array in C/C++, as well as automatically growing the buffer size (if needed) and tracking the length for you. 


2) compare string length 


3) boxing and unboxing ( performance costly)

boxing: convert value type to reference type

unboxing: convert reference type to value type

It's better not using boxing and unboxing because it impact the performance.






references:

http://support.microsoft.com/kb/306822/en-us


C# is a versatile and powerful programming language that has evolved significantly since its introduction. It offers a wide array of features that make it a compelling choice for developers. Below are some key features of the C# programming language, with comparisons to similar features in other languages. ### Object-Oriented Programming C# supports object-oriented programming (OOP) principles, including encapsulation, inheritance, and polymorphism. Classes and objects are fundamental to C# and allow for the creation of modular programs and reusable code[^3]. Similar to Java, C# uses a class-based approach to OOP, where classes are used to define the structure and behavior of objects. ### Type Safety C# is a type-safe language, which means that the compiler ensures that operations are performed on compatible data types. This helps prevent many common programming errors that can occur when working with incompatible types. In contrast, languages like C++ offer more flexibility but at the cost of type safety. ### Garbage Collection C# employs automatic memory management through garbage collection, which helps manage the allocation and release of memory resources. This feature is similar to Java and is in contrast to languages like C and C++, where developers must manually manage memory allocation and deallocation. ### LINQ (Language Integrated Query) C# introduced LINQ, which allows developers to query data directly within the language using a SQL-like syntax. This feature simplifies data manipulation and is integrated into the language itself, making it more intuitive and easier to use compared to external query languages[^2]. ### Partial Classes and Methods C# 2.0 introduced partial classes and methods, allowing the definition of a class to be split across multiple files. This feature is particularly useful in large projects where multiple developers may be working on different parts of the same class simultaneously[^2]. ### Delegates and Events C# supports delegates and events, which are used to implement event-driven programming. Delegates are similar to function pointers in C++ but are type-safe and secure. Events are a special kind of delegate that can be used to notify other objects of changes or actions. ### Generics C# supports generics, which allow for the creation of type-safe collections and methods that can work with any data type. This feature is similar to templates in C++ but provides better type safety and performance. ### Asynchronous Programming C# includes support for asynchronous programming through the `async` and `await` keywords, which simplify the process of writing non-blocking code. This feature is comparable to JavaScript's Promises and async/await syntax. ### Nullable Value Types C# allows value types to be nullable, which means that they can represent the absence of a value. This feature is useful when working with databases or other data sources that may contain null values. ### Code Snippet Example Here is a simple example demonstrating the use of a class in C#: ```csharp public class Person { public string Name { get; set; } public int Age { get; set; } public void Introduce() { Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old."); } } // Usage Person person = new Person { Name = "Alice", Age = 30 }; person.Introduce(); ``` This code defines a `Person` class with properties `Name` and `Age`, along with a method `Introduce` that prints a message to the console. ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值