Linked Questions

17 votes
2 answers
46k views

Adding integers to strings in C# [duplicate]

Recently I have been informed that it is possible to concatenate integers (and other types) to string and vice versa, i.e. // x == "1234" // y == "7890" string x = "123" + 4; string y = 7 + "890"; ...
A.R.'s user avatar
  • 15.5k
0 votes
3 answers
92 views

what does ' "" ' mean in ' "" + ex ' in C#? [duplicate]

I'm new to C# and I always use it but don't understand what it's doing. catch (Exception ex) { return "" + ex; } return "Name : " + result; When I hover over it it says "...
Peter's user avatar
  • 51
2 votes
2 answers
213 views

String concatenation in C# with DateTime objects: why is my code legal? [duplicate]

In some C# code I'm working on, a DateTime object (dt) is concatenated with two strings: string test = "This is a test " + dt + "...Why does this work?" This doesn't raise a compile error and is ...
rookie's user avatar
  • 2,853
0 votes
1 answer
149 views

Why does adding integers to a string result in a string? [duplicate]

I can't understand, why in the first write sentence 5 and 7 are numbers (so the output is 12), but in the last write sentence 5 and 7 are strings (so the output is 57). Is in the C# rule, that if the ...
prahsC's user avatar
  • 21
0 votes
1 answer
62 views

Why does this string get cast to an Object in the DLL? [duplicate]

I was comparing 2 DLL's, which I don't do often, and I was confused when I saw this line: myObj.ErrorString = (string) (object) num + (object) " file(s) failed to import. Run the Import Report for ...
sab669's user avatar
  • 4,064
21 votes
2 answers
703 views

C# concatenation strings while compiling

Please help understand this behavior. When I use this: bool a1 = (object)("string" + 1) == ("string" + 1); The result is false But when I use this bool a2 = (object)("string" + "1") == ("string" + ...
Yuriy Mayorov's user avatar
6 votes
6 answers
733 views

int to string without explicit conversion?

I have a kind a simple question but I'm surprised. This code works: int itemID = 1; string dirPath = @"C:\" + itemID + @"\abc"; Why don't I have to do itemID.ToString() in this case ?
Tony's user avatar
  • 12.6k
10 votes
5 answers
509 views

Why is there no exception when adding null to a string?

Why doesnt this throw an exception dont understand, obj is null object obj = null; Console.WriteLine("Hello World " + obj);
ash 2010's user avatar
  • 329
4 votes
4 answers
2k views

Is C# a type safe language? How about my example

I am testing the following code in C# and it can be run successfully. My question is I can assign one type of data to another type of data in the following example, but why it is still called type-...
user1232250's user avatar
5 votes
4 answers
2k views

How are strings terminated in C#?

This program throws ArrayIndexOutOfBoundException. string name = "Naveen"; int c = 0; while( name[ c ] != '\0' ) { c++; } Console.WriteLine("Length of string " + name + " is: " + c); Why is it ...
Naveen Kumar V's user avatar
5 votes
3 answers
1k views

String Concatenation and Autoboxing in Java

When you concatenate a String with a primitive such as int, does it autobox the value first. ex. String string = "Four" + 4; How does it convert the value to a string in Java?
rubixibuc's user avatar
  • 7,209
1 vote
5 answers
465 views

C# WinForms - Adding two textbox automatically

I'm building this three textbox where if the two textbox are filled the sum will be displayed however, when I input 10 + 10. The result is 1010. Can anbody help me with this? Here's my code: public ...
Noobster's user avatar
-2 votes
4 answers
267 views

Why I don't need ToString() method inside a MessgeBox.Show () [closed]

I just noticed this works too: MessageBox.Show("number of things in the report are " + myHashSetVariable.Count); I was under the impression that I should use myHashSetVariable.Count.ToString() Is ...
Bohn's user avatar
  • 26.5k
0 votes
2 answers
83 views

A want to add a data from mysql table with the sum of a gridview (row), but it shows -use of unassigned local variable 'a' error in vs05, c#?

void opt_gridview1() { string myConnection = "datasource= localhost;port=3306;username=root;password=root"; MySqlConnection myConn = new MySqlConnection(myConnection); MySqlCommand ...
user3672094's user avatar
4 votes
1 answer
105 views

How does the C# compiler resolve types before applying a binary operator?

I'm working on a typed scripting language backed by C# Expression Trees. I'm stuck on one issue around proper type conversion with binary operators. Here is an example of the behavior I'm trying to ...
weichsem's user avatar
  • 276

15 30 50 per page