-1

I have a C# Service from Which I get the data from DataBase using Dapper Repository and I don'have SQL query to append it executes in a single statement For Example:

public CreativeMediaTypeViewModel GetCreativeMediaType(string ContractNumber, int SellerID, int CreativeClassID)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.AppendFormat(@" SELECT DISTINCT PurchasedSiteTypeMediaType.MediaType_bmID, MediaTypeName,Acronym,@CreativeClassID AS CreativeClassID 
                                    FROM PurchasedSiteTypeMediaType INNER JOIN MediaType_bm ON MediaType_bm.MediaType_bmID = PurchasedSiteTypeMediaType.MediaType_bmID
                                    WHERE ContractNumber = @ContractNumber
                                    AND PurchasedSiteTypeMediaType.SellerID = @SellerID
                                    AND MediaType_bm.TrackingMethod <> 'Supplies' ");

            var _parameters  = new DynamicParameters();
            _parameters.Add("@SellerID", SellerID);
            _parameters.Add("@ContractNumber", ContractNumber);
            _parameters.Add("@CreativeClassID", CreativeClassID);

            var result = _creativeDapperRepository.ExecuteQuery<CreativeMediaTypeViewModel>(strSQL.ToString(), _parameters).FirstOrDefault();
            return result;
        }

In this Method Which is better for better performance string or StringBuilder?

Biraz Dahal
  • 361
  • 1
  • 5
  • 20
  • 1
    Does this answer your question? [Performance considerations for String and StringBuilder - C#](https://stackoverflow.com/questions/7068407/performance-considerations-for-string-and-stringbuilder-c-sharp) – stud3nt Jan 07 '20 at 06:29
  • 3
    Does this answer your question? [String vs. StringBuilder](https://stackoverflow.com/questions/73883/string-vs-stringbuilder) – Shahid Manzoor Bhat Jan 07 '20 at 06:29
  • A direct string will be “better” *since there is no additional appending*. SB has more overhead: it requires the initial string *anyway*, maintains an internal buffer that is updated, *and* requires creating a copy of the buffer to a *new string* at the end. It also represents a use case that is not relevant in context. Use SB when there is a *use case* for it - otherwise, use a simpler direct string. (It’s also dubious to use AppendFormat as there is no format to apply.) – user2864740 Jan 07 '20 at 06:48
  • (That is, from a performance viewpoint, SB and the further usage of AppendFormat are “wasteful”. From a coding practice standpoint, they are misleading in intent with AppendFormat being error prone when the behavior it implies is not expected.) – user2864740 Jan 07 '20 at 06:55
  • Only one answer: Measure it, i.e. with Benchmark.net: https://github.com/dotnet/BenchmarkDotNet, but unlikely to make any sense unless you are doing like 100000 or million concats a second. NOt sure why you need a StringBuilder in this case anyways. – Tseng Jan 07 '20 at 07:35

1 Answers1

0

In this case I would say using just a simple string would be better since you do not need to format it at all.

However I do not believe it will make much difference performance wise with what you want to achieve.

Christo Nel
  • 361
  • 3
  • 14