开发者

Converting a line of Java to C#

Can someone explain to me what this li开发者_如何学Pythonne below in Java means and how would I convert it to C#? Thanks.

private class X extends Y<Void, Object, Void> {
.......
}


The class X inherits from the generic class Y. The Y class has three generic parameters. X extends of class Y with parameters Void, Object and Void.

In C# this would look as follows:

private class X : Y<object, object, object> {
}

As mentioned in the comments, void can't be passed as a type, so you have to pass at least object in C#.


This code does look fishy. Class Y needs to be aware of three types, of which two you never want to assign a non-null value to (the void types).


In Java, you can use java.lang.Void as a type argument when you really don't care about that type parameter, and it will always be null. For example:

public interface Command<T> {
  T execute();
}

public class VoidReturningCommand implements Command<Void> {
  public Void execute() {
    // executes ....
    return null; // don't care about the return...
  }
}

In C#, System.Void cannot be used like that.

So, to communicate that the object is really void, you can create an uninstantiatable type for it:

public sealed class Nothingness {
  private Nothingness() { }
}

And then use it:

private class X : Y<Nothingness, object, Nothingness> {

}

null is the only valid value for class Nothingness.

(Other names that I thought for class Nothingness include Null, Nothing - bad for VB.NET -, Void, Unit, Ignored, Mu - you decide which one is best for you)

This carries the intent of the original Java code to C#.


Steven's answer doesn't look quite right - void can't be used this way in C#.

Instead you could represent what is void in the Java implementation as an Action or maybe Func in .NET, as such (omitting any complexities such as constraints):

private class Y<T, K, Z> { }

private class X : Y<Action, object, Action>
{

}

What's happening here is that we're defining X, which is a class in its own right, to inherit the class Y. Y is in fact a generic type (of which the utilisation kind of reduces the need and even desire to use object here) and so also exposes parameters within the angle brackets - this means, for instance, that object could be changed to Fruit and you could instantiate Y with any sub-classing type, such as Apple.

See here for information on inheritance, and here for information on generics.

This whole implementation is kind of backwards, though, unless X is attempting to hide information about the underlying type and its arguments with intent.

A slightly more elaborate example just to illustrate how one might use generics and inheritance may look a little like this:

    public abstract class Fruit { }

    public class Apple : Fruit { }

    public class Pear : Fruit { }

    public class LunchBox<A>
        where A : Fruit
    {
        public A FruitSnack { get; set; }
    }

    public class LunchBox<A, B> : LunchBox<A>
        where A : Fruit 
        where B : Fruit
    {
        public B ExtraFruitSnack { get; set; }
    }

    var myLunchBox = new LunchBox<Apple>();

    var myBiggerLunchBox = new LunchBox<Apple, Pear>();


This means that it is adding functionality onto Class Y which takes some generic object types to instantiate.

In C# this would look like

private class X : Y<null, Object, null>
{

}

This is called inheritance where X is inheriting the attributes of the Base Class Y.

I don't think however that you can pass in a null like that.


Means that a private class X inherits Y class, and Y has 3 generic parameters: Void, Object and Void.

In C# you can't use "void" as a generic type parameter, so it would look like this:

private class X : Y<object, object, object>
{
}

And members that would return void now return a null object. And that's all.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜