TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Maha
NA
0
324.9k
Casting and TrimEnd
Jan 23 2014 10:34 AM
http://www.dotnetperls.com/string-join
Following program is given in the above website. Please explain the reasons for the followings:
-In order to cast builder why it is not correct to use (
string
)builder.
-TrimEnd({ ',' }) is doing the job instead of TrimEnd(
new
char[] { ',' })
Problem is highlited.
using System;
using System.Text;
class Program
{
static void Main()
{
string[] catSpecies = { "Aegean", "Birman", "Main Coon", "Nebulung" };
Console.WriteLine(CombineA(catSpecies));
Console.WriteLine(CombineB(catSpecies));
Console.Read();
}
/// <summary>
/// Combine strings with commas.
/// </summary>
static string CombineA(string[] arr)
{
return string.Join(",", arr);
}
/// <summary>
/// Combine strings with commas.
/// </summary>
static string CombineB(string[] arr)
{
StringBuilder builder = new StringBuilder();
foreach (string s in arr)
{
builder.Append(s).Append(",");
}
return
builder
.ToString().
TrimEnd(new char[] { ',' })
;
}
}
/*
Aegean,Birman,Main Coon,Nebulung
Aegean,Birman,Main Coon,Nebulung
*/
Reply
Answers (
2
)
data to fetch datewise from database in gridview
programming