How to escape message System.Int32 in C#
I'm just beginning to learn C# so I have a question.
Why do I get the message System.Int 32 ?
public static int GetFirstEvenNumbers(int count)
{
int array = new int[count];
for (int i = 1; i <= array.Length; i++)
array[i - 1] = i * 2;
for (int j = 0; j < count; j++)
Console.Write(array[j] + " ");
return new int[count];
}
public static void Main(string args)
{
Console.Write(GetFirstEvenNumbers(5));
Console.ReadKey();
}
c#
add a comment |
I'm just beginning to learn C# so I have a question.
Why do I get the message System.Int 32 ?
public static int GetFirstEvenNumbers(int count)
{
int array = new int[count];
for (int i = 1; i <= array.Length; i++)
array[i - 1] = i * 2;
for (int j = 0; j < count; j++)
Console.Write(array[j] + " ");
return new int[count];
}
public static void Main(string args)
{
Console.Write(GetFirstEvenNumbers(5));
Console.ReadKey();
}
c#
3
Because when you call Console.Write with a value that is not a string, the default behaviour is to output the type of the value.
– stuartd
Nov 14 '18 at 17:52
3
You may use Console.Write(string.Join(", ", GetFirstEvenNumbers(5)));
– Klaus Gütter
Nov 14 '18 at 17:53
1
return new int[count]; should be: return array;
– Poul Bak
Nov 14 '18 at 18:23
Because no compiler/runtime yet built (or ever to be built) is able to takeprint(something)and correctly infer what you want to see. There will be defaults, but no magic
– Damien_The_Unbeliever
Nov 14 '18 at 18:27
add a comment |
I'm just beginning to learn C# so I have a question.
Why do I get the message System.Int 32 ?
public static int GetFirstEvenNumbers(int count)
{
int array = new int[count];
for (int i = 1; i <= array.Length; i++)
array[i - 1] = i * 2;
for (int j = 0; j < count; j++)
Console.Write(array[j] + " ");
return new int[count];
}
public static void Main(string args)
{
Console.Write(GetFirstEvenNumbers(5));
Console.ReadKey();
}
c#
I'm just beginning to learn C# so I have a question.
Why do I get the message System.Int 32 ?
public static int GetFirstEvenNumbers(int count)
{
int array = new int[count];
for (int i = 1; i <= array.Length; i++)
array[i - 1] = i * 2;
for (int j = 0; j < count; j++)
Console.Write(array[j] + " ");
return new int[count];
}
public static void Main(string args)
{
Console.Write(GetFirstEvenNumbers(5));
Console.ReadKey();
}
c#
c#
edited Nov 14 '18 at 17:55
Alex K.
139k21202239
139k21202239
asked Nov 14 '18 at 17:50
Yariy ZhlobYariy Zhlob
62
62
3
Because when you call Console.Write with a value that is not a string, the default behaviour is to output the type of the value.
– stuartd
Nov 14 '18 at 17:52
3
You may use Console.Write(string.Join(", ", GetFirstEvenNumbers(5)));
– Klaus Gütter
Nov 14 '18 at 17:53
1
return new int[count]; should be: return array;
– Poul Bak
Nov 14 '18 at 18:23
Because no compiler/runtime yet built (or ever to be built) is able to takeprint(something)and correctly infer what you want to see. There will be defaults, but no magic
– Damien_The_Unbeliever
Nov 14 '18 at 18:27
add a comment |
3
Because when you call Console.Write with a value that is not a string, the default behaviour is to output the type of the value.
– stuartd
Nov 14 '18 at 17:52
3
You may use Console.Write(string.Join(", ", GetFirstEvenNumbers(5)));
– Klaus Gütter
Nov 14 '18 at 17:53
1
return new int[count]; should be: return array;
– Poul Bak
Nov 14 '18 at 18:23
Because no compiler/runtime yet built (or ever to be built) is able to takeprint(something)and correctly infer what you want to see. There will be defaults, but no magic
– Damien_The_Unbeliever
Nov 14 '18 at 18:27
3
3
Because when you call Console.Write with a value that is not a string, the default behaviour is to output the type of the value.
– stuartd
Nov 14 '18 at 17:52
Because when you call Console.Write with a value that is not a string, the default behaviour is to output the type of the value.
– stuartd
Nov 14 '18 at 17:52
3
3
You may use Console.Write(string.Join(", ", GetFirstEvenNumbers(5)));
– Klaus Gütter
Nov 14 '18 at 17:53
You may use Console.Write(string.Join(", ", GetFirstEvenNumbers(5)));
– Klaus Gütter
Nov 14 '18 at 17:53
1
1
return new int[count]; should be: return array;
– Poul Bak
Nov 14 '18 at 18:23
return new int[count]; should be: return array;
– Poul Bak
Nov 14 '18 at 18:23
Because no compiler/runtime yet built (or ever to be built) is able to take
print(something) and correctly infer what you want to see. There will be defaults, but no magic– Damien_The_Unbeliever
Nov 14 '18 at 18:27
Because no compiler/runtime yet built (or ever to be built) is able to take
print(something) and correctly infer what you want to see. There will be defaults, but no magic– Damien_The_Unbeliever
Nov 14 '18 at 18:27
add a comment |
3 Answers
3
active
oldest
votes
What you Write to the console has type int. This type, int, does not override ToString(). Therefore the behavior it inherits from object is used, and that is to simply return the full name of its type, which is System.Int32.
What you want to do is create a string consisting of all the entries in the array, separated with some symbol. You can use string.Join for that. For example:
var firstFiveEven = GetFirstEvenNumbers(5);
var firstFiveEvenAsStringWithSeparators = string.Join(" - ", firstFiveEven);
Console.Write(firstFiveEvenAsStringWithSeparators);
Console.ReadKey();
add a comment |
what Stuartd said is true:
in your code assuming you want to write each integer of the array and then all the array
in one time there is some changes need to be done to your code
1- returning new empty array or array of zeros in your method GetFirstEvenNumbers need to be replaced by the array variable from your code which hold the multiplied by two values.
2- since your method(function) return array of int then you need to convert that to string so that it could be written by the console a good c# method to use is string.join
so your code after these changes should look something like this:
public static int GetFirstEvenNumbers(int count)
{
int array = new int[count];
for (int i = 1; i <= array.Length; i++)
array[i - 1] = i * 2;
// if you don't need to repeat the result twice get rid off the
// second for loop and Console.Write
for (int j = 0; j < count; j++)
Console.Write(array[j] + " ");
return array;
}
public static void Main(string args)
{
Console.Write(string.Join(",",GetFirstEvenNumbers(5)));
Console.ReadKey();
}
I hope this helps.
add a comment |
The reason for the bahaviour you see is because when you pass an object (regardless of type) to the Console.Write method then it will call the objects ToString() method.
Some types have overridden this method in order to present the object or value in a way that makes sense for the type (for example, an integer would print its value).
Many objects have not overriden this method and thus they default back to Object.ToString() and the behaviour of Object.ToString() is to print out the type of the object.
You can read more about it here.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306091%2fhow-to-escape-message-system-int32-in-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
What you Write to the console has type int. This type, int, does not override ToString(). Therefore the behavior it inherits from object is used, and that is to simply return the full name of its type, which is System.Int32.
What you want to do is create a string consisting of all the entries in the array, separated with some symbol. You can use string.Join for that. For example:
var firstFiveEven = GetFirstEvenNumbers(5);
var firstFiveEvenAsStringWithSeparators = string.Join(" - ", firstFiveEven);
Console.Write(firstFiveEvenAsStringWithSeparators);
Console.ReadKey();
add a comment |
What you Write to the console has type int. This type, int, does not override ToString(). Therefore the behavior it inherits from object is used, and that is to simply return the full name of its type, which is System.Int32.
What you want to do is create a string consisting of all the entries in the array, separated with some symbol. You can use string.Join for that. For example:
var firstFiveEven = GetFirstEvenNumbers(5);
var firstFiveEvenAsStringWithSeparators = string.Join(" - ", firstFiveEven);
Console.Write(firstFiveEvenAsStringWithSeparators);
Console.ReadKey();
add a comment |
What you Write to the console has type int. This type, int, does not override ToString(). Therefore the behavior it inherits from object is used, and that is to simply return the full name of its type, which is System.Int32.
What you want to do is create a string consisting of all the entries in the array, separated with some symbol. You can use string.Join for that. For example:
var firstFiveEven = GetFirstEvenNumbers(5);
var firstFiveEvenAsStringWithSeparators = string.Join(" - ", firstFiveEven);
Console.Write(firstFiveEvenAsStringWithSeparators);
Console.ReadKey();
What you Write to the console has type int. This type, int, does not override ToString(). Therefore the behavior it inherits from object is used, and that is to simply return the full name of its type, which is System.Int32.
What you want to do is create a string consisting of all the entries in the array, separated with some symbol. You can use string.Join for that. For example:
var firstFiveEven = GetFirstEvenNumbers(5);
var firstFiveEvenAsStringWithSeparators = string.Join(" - ", firstFiveEven);
Console.Write(firstFiveEvenAsStringWithSeparators);
Console.ReadKey();
answered Nov 14 '18 at 18:25
Jeppe Stig NielsenJeppe Stig Nielsen
43.1k672130
43.1k672130
add a comment |
add a comment |
what Stuartd said is true:
in your code assuming you want to write each integer of the array and then all the array
in one time there is some changes need to be done to your code
1- returning new empty array or array of zeros in your method GetFirstEvenNumbers need to be replaced by the array variable from your code which hold the multiplied by two values.
2- since your method(function) return array of int then you need to convert that to string so that it could be written by the console a good c# method to use is string.join
so your code after these changes should look something like this:
public static int GetFirstEvenNumbers(int count)
{
int array = new int[count];
for (int i = 1; i <= array.Length; i++)
array[i - 1] = i * 2;
// if you don't need to repeat the result twice get rid off the
// second for loop and Console.Write
for (int j = 0; j < count; j++)
Console.Write(array[j] + " ");
return array;
}
public static void Main(string args)
{
Console.Write(string.Join(",",GetFirstEvenNumbers(5)));
Console.ReadKey();
}
I hope this helps.
add a comment |
what Stuartd said is true:
in your code assuming you want to write each integer of the array and then all the array
in one time there is some changes need to be done to your code
1- returning new empty array or array of zeros in your method GetFirstEvenNumbers need to be replaced by the array variable from your code which hold the multiplied by two values.
2- since your method(function) return array of int then you need to convert that to string so that it could be written by the console a good c# method to use is string.join
so your code after these changes should look something like this:
public static int GetFirstEvenNumbers(int count)
{
int array = new int[count];
for (int i = 1; i <= array.Length; i++)
array[i - 1] = i * 2;
// if you don't need to repeat the result twice get rid off the
// second for loop and Console.Write
for (int j = 0; j < count; j++)
Console.Write(array[j] + " ");
return array;
}
public static void Main(string args)
{
Console.Write(string.Join(",",GetFirstEvenNumbers(5)));
Console.ReadKey();
}
I hope this helps.
add a comment |
what Stuartd said is true:
in your code assuming you want to write each integer of the array and then all the array
in one time there is some changes need to be done to your code
1- returning new empty array or array of zeros in your method GetFirstEvenNumbers need to be replaced by the array variable from your code which hold the multiplied by two values.
2- since your method(function) return array of int then you need to convert that to string so that it could be written by the console a good c# method to use is string.join
so your code after these changes should look something like this:
public static int GetFirstEvenNumbers(int count)
{
int array = new int[count];
for (int i = 1; i <= array.Length; i++)
array[i - 1] = i * 2;
// if you don't need to repeat the result twice get rid off the
// second for loop and Console.Write
for (int j = 0; j < count; j++)
Console.Write(array[j] + " ");
return array;
}
public static void Main(string args)
{
Console.Write(string.Join(",",GetFirstEvenNumbers(5)));
Console.ReadKey();
}
I hope this helps.
what Stuartd said is true:
in your code assuming you want to write each integer of the array and then all the array
in one time there is some changes need to be done to your code
1- returning new empty array or array of zeros in your method GetFirstEvenNumbers need to be replaced by the array variable from your code which hold the multiplied by two values.
2- since your method(function) return array of int then you need to convert that to string so that it could be written by the console a good c# method to use is string.join
so your code after these changes should look something like this:
public static int GetFirstEvenNumbers(int count)
{
int array = new int[count];
for (int i = 1; i <= array.Length; i++)
array[i - 1] = i * 2;
// if you don't need to repeat the result twice get rid off the
// second for loop and Console.Write
for (int j = 0; j < count; j++)
Console.Write(array[j] + " ");
return array;
}
public static void Main(string args)
{
Console.Write(string.Join(",",GetFirstEvenNumbers(5)));
Console.ReadKey();
}
I hope this helps.
answered Nov 14 '18 at 18:26
GeorgeTGeorgeT
114
114
add a comment |
add a comment |
The reason for the bahaviour you see is because when you pass an object (regardless of type) to the Console.Write method then it will call the objects ToString() method.
Some types have overridden this method in order to present the object or value in a way that makes sense for the type (for example, an integer would print its value).
Many objects have not overriden this method and thus they default back to Object.ToString() and the behaviour of Object.ToString() is to print out the type of the object.
You can read more about it here.
add a comment |
The reason for the bahaviour you see is because when you pass an object (regardless of type) to the Console.Write method then it will call the objects ToString() method.
Some types have overridden this method in order to present the object or value in a way that makes sense for the type (for example, an integer would print its value).
Many objects have not overriden this method and thus they default back to Object.ToString() and the behaviour of Object.ToString() is to print out the type of the object.
You can read more about it here.
add a comment |
The reason for the bahaviour you see is because when you pass an object (regardless of type) to the Console.Write method then it will call the objects ToString() method.
Some types have overridden this method in order to present the object or value in a way that makes sense for the type (for example, an integer would print its value).
Many objects have not overriden this method and thus they default back to Object.ToString() and the behaviour of Object.ToString() is to print out the type of the object.
You can read more about it here.
The reason for the bahaviour you see is because when you pass an object (regardless of type) to the Console.Write method then it will call the objects ToString() method.
Some types have overridden this method in order to present the object or value in a way that makes sense for the type (for example, an integer would print its value).
Many objects have not overriden this method and thus they default back to Object.ToString() and the behaviour of Object.ToString() is to print out the type of the object.
You can read more about it here.
edited Nov 14 '18 at 18:26
answered Nov 14 '18 at 18:20
ShaziShazi
686616
686616
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306091%2fhow-to-escape-message-system-int32-in-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Because when you call Console.Write with a value that is not a string, the default behaviour is to output the type of the value.
– stuartd
Nov 14 '18 at 17:52
3
You may use Console.Write(string.Join(", ", GetFirstEvenNumbers(5)));
– Klaus Gütter
Nov 14 '18 at 17:53
1
return new int[count]; should be: return array;
– Poul Bak
Nov 14 '18 at 18:23
Because no compiler/runtime yet built (or ever to be built) is able to take
print(something)and correctly infer what you want to see. There will be defaults, but no magic– Damien_The_Unbeliever
Nov 14 '18 at 18:27