Casting base class to derived class [duplicate] - c#

This question already has answers here:
C# : Converting Base Class to Child Class
(12 answers)
Closed 3 years ago.
Basically there're 2 classes.
class Motor {
int a;
int b;
int c;
}
class Title : Motor {
int d;
int e;
}
Now a function is passed with an instance of Motor class.
bool AssignNums(Motor m)
{
Title t = (Title)m; //throws exception
//Do other things with "t"
}
And it's called from,
void AddNums()
{
Motor m = new Motor();
m.a = 23;
m.b = 78;
m.c = 109;
AssignNums(m);
}
The above line where the casting is done, doesn't work. It throws a null pointer exception.
I tried:
bool AssignNums(Motor m)
{
Title t = new Title();
t = (Title)m; // Doesn't work.
//Do other things with "t"
}
The above approach also doesn't work.
Coming from a C++ background, it's kinda difficult to understand how casting works in C#.
In C++ below code will work.
bool AssignNums(Motor* m)
{
Title* t = (Title*)m; //Will work
//Do other things with "t"
}
How can this be done in C#? Without tons of code to implement "reflection" or something like that...

In C#, you can't cast an object to a more specific subclass. Imagine what could happen if it were possible:
class Foo : Motor
{
int z;
}
Foo m = new Foo();
bool AssignNums(Motor m)
{
Title t = (Title)m; // Pretend it doens't throw an exception
//Do other things with "t"
t.d = 42; // Oopps! The actual object is Foo, not Title, so there is no field called d
// InvalidCastException is thrown at the line that attempts the cast
}
You can convert it at runtime if the actual type is compatible with the desired one:
bool AssignNums(Motor m)
{
Title t = m as Title;
if (t != null)
{
// Do stuff with t
}
}
Beginning with C# 7, you can use the shorter form
bool AssignNums(Motor m)
{
if (m is Title t)
{
// Do stuf with t
}
}
If handling multiple subclasses, you can also use switch..when
switch (m)
{
case Title t when m is Title:
// Do something with t
break;
case Foo f when m is Foo:
// Do something with f
break;
}
Note, though, if you're branching based on the subclass present at runtime, it's a sign that you may have a design issue.

Use the other C# casting syntax, and use a recent C# feature (that allows associating a variable to the cast immediately):
bool AssignNums(Motor m)
{
if (m is Title t) {
//Do things with "t"
}
}
If you do it this way, you ignore incoming m instances that are not of type Title. The is cast also never throws (neither does the related as operation).

Related

How to check the type of an Object (including boxed scalar) in WinRT + C++/CX

I have a C# function that returns an arbitrary Object (which can be a string, a dictionary, or any other class, including boxed numbers) e.g.
public static Object foo() {
return 3.14;
}
When called from C++/CX on WinRT how can I know check the type of the returned object? I don't understand why this doesn't work:
if (result->GetType() == String::typeid)
if (result->GetType() == double::typeid)
if (result->GetType()->Equals(double::typeid))
This does work for "real" objects:
auto temp = dynamic_cast<String^>(result);
if (temp) {
...
}
But I can't figure out how to handle the boxed number case since dynamic_cast<Double>(result) is not possible.
PS: I saw this similar question but it doesn't cover the boxed number case.
After quite a bit of Googling I found this solution but I have no idea if that's proper:
Object^ result = ...
if (dynamic_cast<IBox<double>^>(result)) {
auto value = safe_cast<double>(result);
...
}
auto string = dynamic_cast<String^>(result);
if (string) {
auto value = _PlatformStringToString(string);
...
}

Possible to get reference of "operator" result?

Is it somehow possible to get the reference of the result of an overloaded operator in C# so you don't have to use the "new" keyword to create a temp result (which is returned afterwards)?
Here's an example of a problem I ran into:
public class Stats {
public float someField;
public float someOtherField;
public static Stats operator +(Stats a, Stats b) {
Stats c = new Stats(); // I don't want a new one, can I access operators result directly?
c.someField = a.someField + b.someField;
c.someOtherField = a.someOtherField + b.someOtherField;
return c;
}
/*
// This is what I want to achieve, but it would be cooler if static and with the "+"
public Add(SomeType a) {
someField += a.someField;
someOtherField += a.someOtherField
}
*/
}
public class StatObserver {
public Stats statsToObserve;
public Output() {
print(statsToObserve.someField);
}
}
public class Class {
public Stats firstStats = new Stats();
firstStats.someField = 1.5f;
public StatObserver showStats = new StatObserver();
showStats.statsToObserve = firstStats;
public Stats nextStats = new Stats();
nextStats.someField = 3.4f;
// now the tricky part
firstStats += nextStats; // C# handles the += itself correctly
showStats.Output(); // prints "1.5"
// you have to update the observer to get the new value
// it's kind of stupid, because you have to treat firstStats like a value type buts its not
showStats.statsToObserve = firstStats;
showStats.Output(); // prints "4.9"
}
You can't overload the += operator directly - it is compiled to an add and an assignment. You could mutate the left-hand side as part of the + operator - but that would be evil. An Add method seems to be the cleanest design IMHO.
First things first, as #D.Stanley notes, you can't override +=. You can override +, as you have done, but there is something important to realize about +:
Math operators are non-destructive, that is, they return a result
without modifying the operands
That being said, you could modify the properties of operands in a reference type (which this is), but you shouldn't. So don't. The good news is, your + operation is correct.
Because of this, you have to return a new object (as you do), but when you do the += you assign the local reference to this new object, while leaving the observer's reference pointing at the old object, causing your error.
You probably want to modify the observer directly:
showStats.statsToObserve += nextStats;
Or, you could totally hack it and do this (not recommended):
public static Stats operator +(Stats a, Stats b) {
Stats c = new Stats();
c.someField = a.someField + b.someField;
a.SomeField += b.someField; //AHHHH You just modified an operand!
c.someOtherField = a.someOtherField + b.someOtherField;
a.someOtherField += b.someOtherField; //AHHHH You just did it again!
return c;
}
Note: I like #DStanley's recommendation as well for the solution to this.

Generic Sqrt Implementation

I'm using the MiscUtils library (thanks Marc G. and Jon S.) and am trying to add a generic Sqrt function to it. The problem can be easily reproduced with this:
class N<T>
{
public N(T value)
{
Value = value;
}
public readonly T Value;
public static implicit operator T(N<T> n)
{
return n.Value;
}
public static implicit operator N<T>(T value)
{
return new N<T>(value);
}
public static T operator /(N<T> lhs, T rhs)
{
// Operator.Divide is essentially a wrapper around
// System.Linq.Expressions.Expression.Divide
return Operator.Divide(lhs.Value, rhs);
}
}
// fails with: No coercion operator is defined
// between types 'System.Double' and 'N`1[System.Single]'.
var n = new Numeric<float>(1f);
var x = Operator.DivideAlternative(n, 1.0);
// this works as the N<T> is first converted to a
// float via the implicit conversion operator
var result = n / 1.0;
Now, I realize why this is happening, but I have not yet been able to think of a way around it. For reference, here is the current Sqrt implementation. I have little experience building expression trees.
public static double Sqrt<T>(T value)
{
double oldGuess = -1;
double guess = 1;
while(Abs(guess - oldGuess) > 1)
{
oldGuess = guess;
// the first evaluated call to DivideAlternative throws
guess = Operator.Divide(
Operator.AddAlternative(guess,
Operator.DivideAlternative(value, guess)),
2);
}
return guess;
}
EDIT: Ok, so I solved this on my own, but in an attempt to keep the question as simple as possible I apparently went too far and spent far too much time answering questions from confused people trying to help.
So, this is the problem in its entirety.
I two classes; one that performs transformations and another which performs statistical analysis of image data (pixels). Let's focus on the latter as the problem is the same:
abstract class ImageStatistics
{
private readonly object _pixels;
public ImageStatistics(object pixelArray)
{
Pixels = pixelArray;
}
// calculate the standard deviation of pixel values
public double CalcStdDev();
}
The array of pixels can be any numeric type. In practice, it will be either float, int, ushort, or byte. Now, because generics cannot do things like this:
public T Add<T>(T lhs, T rhs)
{
return lhs + rhs; // oops, no operator + for T
}
I cannot perform any sort of statistical analyses on the pixel values themselves without casting to the proper array type. So, I need to have N sub-classes of ImageProcessor to support N pixel types.
Well, that sucks. I would love to just have a generic ImageProcessor<T> class which has a T[] of pixel data. So, I looked into the MiscUtils library which would allow just this.
Math.Sqrt needs a double, so why not just provide it with one?
public static double Sqrt<T>(T value)
{
return Math.Sqrt(Convert.ToDouble(value));
}
You might also consider casting to dynamic.
public static double Sqrt<T>(T value)
{
return Math.Sqrt((dynamic) value);
}
This technique can also be used for operators like addition:
public static T Add<T>(T a, T b)
{
return (dynamic) a + (dynamic) b;
}
Console application creating array of objects (unknown types) and calculating square route (double)
using System;
namespace GenericSqrt
{
class Program
{
static void Main(string[] args)
{
var array = new object[] { "2", null, 4.1f, 4.444D, "11.3", 0, "Text", new DateTime(1, 1, 1) };
foreach (var value in array)
{
try
{
Console.WriteLine(Sqrt(value));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Console.ReadLine();
}
private static double Sqrt(object value)
{
double converterValue = Convert.ToDouble(value);
return Math.Sqrt(converterValue);
}
}
}
Output looks like this:
1.4142135623731
0
2.02484564958235
2.10807969488822
3.36154726279432
0
Input string was not in a correct format.
Invalid cast from 'DateTime' to 'Double'.
If type is indeed any of number types, as you stated, there is no problem to solve.
Let me preface this by saying it probably isn't worth the effort, considering how this code would need to be maintained. I wrote this in about 10 minutes, so don't expect anything too spectacular.
// You'll need this
public interface ISquareRootHelper
{
double Sqrt<T>(T value)
where T : struct;
}
class Program
{
private static ISquareRootHelper helper;
// Build the helper
public static void BuildSqrtHelper()
{
// Let's use a guid for the assembly name, because guid!
var assemblyName = new AssemblyName(Guid.NewGuid().ToString());
// Blah, blah, boiler-plate dynamicXXX stuff
var dynamicAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
var dynamicModule = dynamicAssembly.DefineDynamicModule(assemblyName.Name);
var dynamicType = dynamicModule.DefineType("SquareRootHelper");
// Let's create our generic square root method in our dynamic type
var sqrtMethod = dynamicType.DefineMethod("Sqrt", MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual);
sqrtMethod.SetReturnType(typeof(double));
// Well, I guess here is where we actually make the method generic
var genericParam = sqrtMethod.DefineGenericParameters(new[] {"T"});
genericParam[0].SetGenericParameterAttributes(GenericParameterAttributes.NotNullableValueTypeConstraint);
// Add a generic parameter, and set it to override our interface method
sqrtMethod.SetParameters(genericParam);
dynamicType.DefineMethodOverride(sqrtMethod, typeof(ISquareRootHelper).GetMethod("Sqrt"));
// Magic sauce!
var ilGenerator = sqrtMethod.GetILGenerator();
// Math.Sqrt((double)value);
ilGenerator.Emit(OpCodes.Ldarg_1); // arg_0 is this*
ilGenerator.Emit(OpCodes.Conv_R8);
var mathSqrtMethodInfo = typeof(Math).GetMethod("Sqrt");
ilGenerator.EmitCall(OpCodes.Call, mathSqrtMethodInfo, null);
ilGenerator.Emit(OpCodes.Ret);
// Since we're overriding the interface method, we need to have the type
// implement the interface
dynamicType.AddInterfaceImplementation(typeof(ISquareRootHelper));
// Create an instance of the class
var sqrtHelperType = dynamicType.CreateType();
helper = (ISquareRootHelper)Activator.CreateInstance(sqrtHelperType);
}
public static void Main(string[] args)
{
BuildSqrtHelper();
Console.WriteLine(helper.Sqrt((short)64)); // Works!
Console.WriteLine(helper.Sqrt((ushort)64)); // Works!
Console.WriteLine(helper.Sqrt((int)64)); // Works!
Console.WriteLine(helper.Sqrt((uint)64)); // Works*!
Console.WriteLine(helper.Sqrt((byte)64)); // Works!
Console.WriteLine(helper.Sqrt((sbyte)64)); // Works!
Console.WriteLine(helper.Sqrt((float)64)); // Works!
Console.WriteLine(helper.Sqrt((double)64)); // Works!
Console.WriteLine(helper.Sqrt((long)64)); // Works!
Console.WriteLine(helper.Sqrt((ulong)64)); // Works*!
// Let's try non-primitives!
Console.WriteLine(helper.Sqrt(DateTime.Now)); // Doesn't fail, but doesn't actually work
Console.WriteLine(helper.Sqrt(Guid.NewGuid())); // InvalidProgramException!
}
}
Anyway, I guess this proves it can be done. Just make sure when you use it, you only pass in primitive types, otherwise all fail will break loose. Actually, it will only throw an exception when you pass in a struct that is a greater size that 8 bytes, since that will unbalance the stack. You can't do a check like sizeof(T) in the method though, because it would fail during the JITing process.
Also, there are some *s next to some of the types up there. There is some extra logic done by the compiler and/or Math.Sqrt when you pass in unsigned numbers vs signed numbers, and how this related to negative numbers. For example:
Console.WriteLine(Math.Sqrt(unchecked((uint)-2))); // 65535.9999847412
Console.WriteLine(helper.Sqrt(unchecked((uint)-2))); // NaN :(
You could improve it and checks above to catch that, though. Also, I don't recommend this solution, especially if you're not comfortable with IL. Plus, this is probably more verbose and complicated than just writing a bunch of different methods to handle the operations you want.
This works, but it's a bit ugly:
public static implicit operator Numeric<T>(double value)
{
return new Numeric<T>((T)Convert.ChangeType(value, typeof(T)));
}
public static implicit operator double(Numeric<T> n)
{
return Convert.ToDouble(n.Value);
}
It will have to be repeated for each supported type, which makes this a lot less generic. I slapped an IConvertible constraint on there for good measure. If anyone has a better solution I'm all ears.

Closures and java anonymous inner classes

Would anyone be so kind to post the equivalent Java code for a closure like this one (obtained using C#) with anonymous inner classes?
public static Func<int, int> IncrementByN()
{
int n = 0; // n is local to the method
Func<int, int> increment = delegate(int x)
{
n++;
return x + n;
};
return increment;
}
static void Main(string[] args)
{
var v = IncrementByN();
Console.WriteLine(v(5)); // output 6
Console.WriteLine(v(6)); // output 8
}
Furthermore, can anyone explain how partial applications can be obtained if lexical closures are available and viceversa? For this second question, C# would be appreciated but it's your choice.
Thanks so much.
There is no closure yet in Java. Lambda expressions are coming in java 8. However, the only issue with what you're trying to translate is that it has state, which not something that lamba expressions will support i don't think. Keep in mind, it's really just a shorthand so that you can easily implement single method interfaces. You can however still simulate this I believe:
final AtomicInteger n = new AtomicInteger(0);
IncrementByN v = (int x) -> x + n.incrementAndGet();
System.out.println(v.increment(5));
System.out.println(v.increment(6));
I have not tested this code though, it's just meant as an example of what might possibly work in java 8.
Think of the collections api. Let's say they have this interface:
public interface CollectionMapper<S,T> {
public T map(S source);
}
And a method on java.util.Collection:
public interface Collection<K> {
public <T> Collection<T> map(CollectionMapper<K,T> mapper);
}
Now, let's see that without closures:
Collection<Long> mapped = coll.map(new CollectionMapper<Foo,Long>() {
public Long map(Foo foo) {
return foo.getLong();
}
}
Why not just write this:
Collection<Long> mapped = ...;
for (Foo foo : coll) {
mapped.add(foo.getLong());
}
Much more concise right?
Now introduce lambdas:
Collection<Long> mapped = coll.map( (Foo foo) -> foo.getLong() );
See how much nicer the syntax is? And you can chain it too (we'll assume there's an interface to do filtering which which returns boolean values to determine whether to filter out a value or not):
Collection<Long> mappedAndFiltered =
coll.map( (Foo foo) -> foo.getLong() )
.filter( (Long val) -> val.longValue() < 1000L );
This code is equivalent I believe (at least it produces the desired output):
public class Test {
static interface IncrementByN {
int increment(int x);
}
public static void main(String[] args) throws InterruptedException {
IncrementByN v = new IncrementByN() { //anonymous class
int n = 0;
#Override
public int increment(int x) {
n++;
return x + n;
}
};
System.out.println(v.increment(5)); // output 6
System.out.println(v.increment(6)); // output 8
}
}
Assuming we have a generic function interface:
public interface Func<A, B> {
B call A();
}
Then we can write it like this:
public class IncrementByN {
public static Func<Integer, Integer> IncrementByN()
{
final int n_outer = 0; // n is local to the method
Func<Integer, Integer> increment = new Func<Integer, Integer>() {
int n = n_outer; // capture it into a non-final instance variable
// we can really just write int n = 0; here
public Integer call(Integer x) {
n++;
return x + n;
}
};
return increment;
}
public static void main(String[] args) {
Func<Integer, Integer> v = IncrementByN();
System.out.println(v.call(5)); // output 6
System.out.println(v.call(6)); // output 8
}
}
Some notes:
In your program, you capture the variable n by reference from the enclosing scope, and can modify that variable from the closure. In Java, you can only capture final variables (thus capture is only by value).
What I did here is capture the final variable from the outside, and then assign it into a non-final instance variable inside the anonymous class. This allows "passing info" into the closure and at the same time having it be assignable inside the closure. However, this information flow only works "one way" -- changes to n inside the closure is not reflected in the enclosing scope. This is appropriate for this example because that local variable in the method is not used again after being captured by the closure.
If, instead, you want to be able to pass information "both ways", i.e. have the closure also be able to change things in the enclosing scope, and vice versa, you will need to instead capture a mutable data structure, like an array, and then make changes to elements inside that. That is uglier, and is rarer to need to do.

C#: GetHashCode, Equals and Dictionary leads to IndexOutOfRangeException? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
When does a dictionary throw an IndexOutOfRangeException on Add or ContainsKey?
This one is funky. I can't get to the bottom of it with ease. Found an exception in the log and dug out some old code. Don't ask me about why this is written this way because I have no idea. The question really is what could be conditions for IndexOutOfRangeException to be thrown when the Item of the dictionary is being set. Here is how the thing looks:
public MyEnum { Undefined = 0, B = 1, C = 2, D = 16, All = B | C | D }
public class MC
{
private int _hashCode;
private int _i;
private MyEnum _e;
public MC(int i, MyEnum e)
{
_i = i;
_e = e;
SetHashCode();
}
private SetHashCode()
{
_hashCode = _i * 31 + (int)e;
}
public override bool Equals(object other)
{
if (!(obj is MC))
{
return false;
}
MC other = (MC)obj;
return ((_i == other._i) && (_e == other._e));
}
public override int GetHashCode()
{
return _hashCode;
}
}
...
var d = new Dictionary<MC, DateTime>();
...
// Create and populate the list of MCs
var mcs = new List<MC>();
...
foreach (MC mc in mcs)
{
...
d[mc] = DateTime.UtcNow; // Pukes here
}
And the exception is:
System.IndexOutOfRangeException, Source: mscorlib Index was outside the bounds of the array. at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
Ideas how to make the line to fail? Don't what to diverge your attention to the wrong direction but I thought there was something fishy with the Equals and GetHashCode but I couldn't prove it so far - no repro using a unit testing framework.
The error you are getting is often caused by multi-threaded, un-locked concurrent access to a dictionary.
See: When does a dictionary throw an IndexOutOfRangeException on Add or ContainsKey?

Categories