Reputation: 2741
I was porting a java code to C# and came across this code. I can't figure how this can be ported, as it seems to declare class arrays. My knowledge in C# is too limited to figure out how this action is achieved in C#. Is there an equivalent to this Stub declaration in C#, and how would it look like? I shrank it to the minimum, because it was declaring 10 objects. Here's how looks the code:
public interface IcodecWeightFunctionStub
{
void hcodec_weight_func(int[] block, int block_offset, int stride, int log2_denom, int weight, int offset);
}
public interface IHcodecBiWeightFunctionStub
{
void hcodec_biweight_func(int[] dst, int dst_offset, int[] src, int src_offset, int stride, int log2_denom, int weightd, int weights, int offset);
}
public IHcodecWeightFunctionStub[] weight_hcodec_pixels_tab = new IHcodecWeightFunctionStub[] {
new IHcodecWeightFunctionStub() {
public void hcodec_weight_func(int []block, int block_offset, int stride, int log2_denom, int weight, int offset) {
weight_hcodec_pixels_c(16, 16, block, block_offset, stride, log2_denom, weight, offset);
}
},
new IHcodecWeightFunctionStub() {
public void hcodec_weight_func(int []block, int block_offset, int stride, int log2_denom, int weight, int offset) {
weight_hcodec_pixels_c(16, 8, block, block_offset, stride, log2_denom, weight, offset);
}
}
};
The problem is not about instantiating interfaces in C#, but more about returning new classes in C#.
Upvotes: 1
Views: 178
Reputation: 6542
Your example is a little inconsistent - the interface method names don't match, but hopefully my array example and C# equivalent will help. You could use delegates and lambdas, but there is no need to do this. For the following Java code:
public interface Foo
{
void Bar(int i);
}
class test
{
//array of anonymous inner classes:
public Foo[] x = new Foo[] {
new Foo() {
public void Bar(int i) {
//code 1
}
},
new Foo() {
public void Bar(int i) {
//code 2
}
}
};
}
The following C# code is equivalent:
public interface Foo
{
void Bar(int i);
}
class Test
{
public Foo[] x = new Foo[] { new FooAnonymousInnerClassHelper1(), new FooAnonymousInnerClassHelper2() };
private class FooAnonymousInnerClassHelper1 : Foo
{
public virtual void Bar(int i)
{
//code 1
}
}
private class FooAnonymousInnerClassHelper2 : Foo
{
public virtual void Bar(int i)
{
//code 2
}
}
}
Upvotes: 1
Reputation: 726899
Anonymous delegate implementations and lambdas of C# are the closest thing to anonymous interface implementations of Java, but the interface needs to have a single method. Both your interfaces have a single method, so your code can be converted to C# as follows:
public delegate void HcodecWeightDelegate(int[] block, int block_offset, int stride, int log2_denom, int weight, int offset);
public delegate void HcodecBiweightDelegate(int[] dst, int dst_offset, int[] src, int src_offset, int stride, int log2_denom, int weightd, int weights, int offset);
public HcodecBiweightDelegate[] WeightHcodecPixelsTab = new HcodecBiweightDelegate[] {
(block, block_offset, stride, log2_denom, weight, offset) => {
weight_hcodec_pixels_c(16, 16, block, block_offset, stride, log2_denom, weight, offset);
}
, (block, block_offset, stride, log2_denom, weight, offset) => {
weight_hcodec_pixels_c(16, 8, block, block_offset, stride, log2_denom, weight, offset);
}
};
Note that calling the delegates is different, too: in Java you would call them like this:
weight_hcodec_pixels_tab[i].hcodec_weight_func(block, block_offset, stride, log2_denom, weight, offset);
In C#, you do not have a method name (because delegates encapsulate a single method), so the same call would look like this:
HcodecBiweightDelegate[i](block, block_offset, stride, log2_denom, weight, offset);
Upvotes: 3