Replies: 4 comments 13 replies
-
I'm assuming that your request is to be able to write this? if (ineed_A)
{
x.A_Value = 12;
}
else
{
x.B_Value = 15; // No casting
} I don't think that's actually stated anywhere. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
i didn't know the "as" operator is faster than castclass https://www.codeproject.com/Articles/8052/Type-casting-impact-over-execution-performance-in public class clsA
{
public int A_Value;
}
public class clsB : clsA
{
public int B_Value;
}
public class C {
static void Main(bool ineed_A)
{
clsA x;
if (ineed_A)
{
x = new clsA();
}
else
{
x = new clsB();
}
x.A_Value = 12;
if (ineed_A)
{
x.A_Value = 12;
}
else
{
((clsB)x).B_Value = 15; // casting is slow so don't
(x as clsB).B_Value = 15; // fast and speed is very close to no casting
}
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Copy Of : dotnet/roslyn#52889
Brief description:
when a class is inherit and i'm sure will use parent but not sure use child that may have some sort of casting
fo example:
this is possible to ignore casting with two variables:
but i think a developers likes to write first example because of readability and maintainability of code, so i have optimization request if this is possible.
Languages applicable:
C#
note: i have no idea how c# would/wouldn't fix this.
Beta Was this translation helpful? Give feedback.
All reactions