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
Tangara G
NA
298
93.6k
can someone explain to me why I can mix string with int here
Nov 8 2016 2:11 AM
Dear experts,
I am stumped by the following code :
using
System;
/// <summary>
/// Implements overloaded indexers.
/// </summary>
class
OvrIndexer
{
private
string[] myData;
private
int
arrSize;
public
OvrIndexer(
int
size)
{
arrSize = size;
myData =
new
string[size];
for
(
int
i=0; i < size; i++)
{
myData[i] =
"empty"
;
}
}
public
string
this
[
int
pos]
{
get
{
return
myData[pos];
}
set
{
myData[pos] = value;
}
}
public
string
this
[string data]
{
get
{
int
count = 0;
for
(
int
i=0; i < arrSize; i++)
{
if
(myData[i] == data)
{
count++;
}
}
return
count.ToString();
}
set
{
for
(
int
i=0; i < arrSize; i++)
{
if
(myData[i] == data)
{
myData[i] = value;
}
}
}
}
static
void
Main(string[] args)
{
int
size = 10;
OvrIndexer myInd =
new
OvrIndexer(size);
myInd[9] =
"Some Value"
;
myInd[3] =
"Another Value"
;
myInd[5] =
"Any Value"
;
myInd[
"empty"
] =
"no value"
;
Console.WriteLine(
"\nIndexer Output\n"
);
for
(
int
i=0; i < size; i++)
{
Console.WriteLine(
"myInd[{0}]: {1}"
, i, myInd[i]);
}
Console.WriteLine(
"\nNumber of \"no value\" entries: {0}"
, myInd[
"no value"
]);
}
}
How come myDate = new String[size] ?
and then size can become an integer again in the for-loop that comes next...
Just can't see the logic behind.
Hope someone can help me out on that .
Tks.
Reply
Answers (
2
)
Why use interface?
Make a freeze program in C#