What is the meaning of the word "Append" [closed] - c#

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I'm in the middle in studying some code and I encountered this word "Append" and I don't understand what it does.
Code:
public static void appendData(string data)
{
if (isRecording) sb.Append(data + Environment.NewLine);
}
What does append mean?

The answer from ChrisF is correct as far as StringBuilder.Append is concerned.
In general, the word "Append" means "to add to the end of". See http://en.wiktionary.org/wiki/append.

I would point out that the "right" way to do that bit of code is:
public static void appendData(string data)
{
if (isRecording)
{
sb.Append(data);
sb.Append(Environment.NewLine);
}
}
Append is doing the same job as string1 + string2 but it is doing it in a much more efficient manner. Look up "Immutable Strings C#" for some more details if you need them.

This is quite simple. Then code above is simply "adding" or "appending" the variables/text supplied within the brackets to the variable "sb".
Append can be found as part of the System.Text.StringBuilder class which I believe is being used above.
More info can be found following this link: StringBuilder Class
Happy coding!

I would guess that sb is of type StringBuilder.
Append() adds the supplied string to the end of the string being built in the StringBuilder variable.

It will add the string representation of the object to end of the string builder instance. It basically calls the .ToString() method of whatever object you pass in and concatenates it to the end of the internal string being build up.
See MSDN documentation

Assuming you are using Visual Studio put your cursor on the word Append and Press F1, you'll probably see something like this. If you are considering refactoring this and assuming it is using a StringBuilder, you might also want to read about AppendLine.

Related

cannot convert from 'char[]' to 'string[]' [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I was trying to make a save dialog from a rich text document but I keep getting an error on the text.
Error:
ArgumentNullException
Argument 2: cannot convert from 'char[]' to 'string[]'
I'm new to c# so I'm not sure how to fix this.
System.IO.File.WriteAllLines(ofd.FileName.ToString(), richTextBox1.Text.ToArray());
Use a different method:
System.IO.File.WriteAllText(ofd.FileName, richTextBox1.Text);
I'd recommend avoiding to use Lines for this; it's a waste of resources to split the text into N strings only to write them all back to a combined file
Why didn't your first attempt work? WriteAllLines requires an array of strings. If you call .ToArray() on a string you get an array of characters; strings and characters are very different things.
ToArray() is a LINQ method that works because a string can be treated as an enumerable sequence of char. Typically it's very rare that you would do so and you would probably use the dedicated string.ToCharArray() method if you did want a char array from a string. Mostly I use it when splitting strings on multiple characters: someString.Split(".,?!'-/:;()".ToCharArray()); as it's more readable than putting each char separately
You're more likely to use ToArray() later on for things like filtering one array using LINQ and turning the result into an array again:
Person[] smiths = people.Where(person => person.LastName == "Smith").ToArray();
Other points:
OpenFileDialog's FileName is already a string; you don't need to ToString() it
Please get into the habit of renaming your controls after you add them to a form (top of the properties grid, the (Name) line, takes two seconds). It's a lot easier for us on the internet (and you in 3 weeks' time) to be able to read usernameTextBox and go "oh, that's the username textbox" than it is to be wondering "is it textbox56 or textbox27 that is the username? I'll just go into the form designer and check.."

List.Reverse() without any effect in C# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Given the Following code:
List<Digit> tempDigits = input.Digits;
string test = tempDigits.ToString(); // -> {2,2,7}
tempDigits.Reverse();
string test2 = tempDigits.ToString(); // -> {2,2,7}
This is my code:
and that the result (in between, I renamed the variable according to naming convention as suggested by Thiessen)
For some reason, the reverse soes not seem to do its job.
It must be something really simple. But I cant get what the Issue is. I hope, someone can help out.
The code you've posted would work correctly, in a vacuum. (Except, in what world does List<>.ToString() produce "{2,2,7}"?)
My main guess would be, as Lesiak commented, that this.Digits and input.Digits points to the exact same List<> instance. So Digits1.Reverse() reverses the list and Digits2.Reverse() reverses the reversal, putting it back how it started. Perhaps this and input are the same thing. Or perhaps they are two different things that happen to use the same underlying Digits list. Who knows?
There are many other possibilities, some of which would be impossible to guess at based on the information provided. For example:
Maybe the input control that's giving you that list is trying to actively manage the list, and reorders it as soon as it notices it's been reversed.
Maybe you're not using the System.Collections.Generic.List<> type, but instead you're referencing some other namespace where someone has written a List<> type that doesn't behave how you'd think it would. Maybe it doesn't even implement Reverse(), so the compiler is picking up on the Enumerable.Reverse() extension method, which does nothing to mutate the underlying data.
It really is impossible to know for sure without knowing more about your environment.
The information you give is incomplete to address the issue.
A reproducible example from the information you provide does not show the same symptoms.
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<int> digits = new List<int> {2,2,7};
foreach(int digit in digits)
Console.Write(digit);
Console.WriteLine();
digits.Reverse();
foreach(int digit in digits)
Console.Write(digit);
Console.WriteLine();
}
}
Which gives the expected output
227
722
https://dotnetfiddle.net/Hs9H0k
My guess would be, this.Digits is not a mutable reference. But that is just an hypothesis.

Generating folders using textboxes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hello experts, I have to generate series of folders from a TextBox into specified location.I am having two textboxes to specify the limit of folders(say 30 folders).The problem am facing is that the folder names that i will be providing are alpha-numeric(say 121cs3h101) .
How to set limit when i provide an alpha-numeric values?
(For example: i provide textbox1=12cs3h101 and textbox2=12cs3h131 , i need the series limit to be generated). I am working with visual studio 2013 in c# windows form application. Thanks in advance.
ok I will try to give you a lead.
To parse a string or find specific characters one can use RegEx.Match or a simler method called String.Split. In both cases you have to be aware how your string is structured and how it can vary. The limits of variation are very important.
If as you say the beginning is always"12cs3h" you can either split the string at the character 'h'.
string[] sa = s.Split('h');
Or you can even use the index of 'h' (since the length seems to be fixed) and take the rest of the string to get the numbers.
int index = s.IndexOf('h');
The rest is up to you, ... convert, enumerate and so on.
EDIT: There is a nice method that does the enumeration job for you: Enumerable.Range Good luck

Unit test for C# interview - Write a method that finds the two-character string that is repeated in the given string most times [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I hope it's OK to ask this question here.
I was asked a question at the interview - with a description in the title.
For example, for given string "ABABDCABDCDDPPABAB" - it would return AB because it appears most times.
Even though I could answer the question - second part of the question was to write a unit test for this method, which would be able to test all possible scenarios!
Could anyone point me on how to tackle such question?
You should answer some questions relates to proposed behavior of the function. For example:
What if source string is null?
What if source string is shorter than two characters?
What if source string contains two or more pairs of characters with the same frequency?
All of you answers you can rewrite as a test methods:
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void FindMostFrequentPair_WithNull_ThrowsArgumentNullException()
{
var pair = Utils.FindMostFrequestPair(null);
}
[TestMethod]
public void FindMostFrequentPair_WithSameFrequentPairs_ReturnsFirst()
{
var pair = Utils.FindMostFrequestPair("ABCD");
Assert.AreEqual("AB", pair);
}
and so on.
Test boundary cases, i.e. null and empty strings, strings with odd number of characters, etc. Also test obvious successful case — you function should work.
The answer would be to test the different possibilities such as:
Testing when AB appears the most
Testing when AB appears the least
Testing when AB appears the same amount as PA
Testing what happens with unexpected input like a null string.
There are probably more options but that would need a look at the code before going into them like verifying certain methods are called etc etc

Writing a recursive function in C# to create a string from characters [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
Currently, I have something like the following:
public char functName(int n)
{
some functionality....
if(condition1)
return convertFuncToChar(variable) + functName(modifiedNumber);
else
return convertFuncToChar(variable);
}
however, I realize that doesn't give me a string (and the syntax shows that there's an error).
I know that for c++, I would most likely use char* to initialize the function, but this is C#.
I don't think it works if I initialize with String either.
Assuming I understand the question correctly, you can take advantage of two things:
1) all basic types implement .ToString(), which creates a string for you
2) string implements operator+()
The following is an example of a recursive function that will recursively concatenate characters to create a string. The output is the number in reverse as a string.
static string funcName(int n)
{
if (n<10)
return (n%10).ToString();
return (n%10).ToString() + funcName(n/10);
}
Of course, it would be more efficient to write this non-recursively.

Categories

Resources