Home > Net >  Consult: contains the class of the generic attributes, which way is better?
Consult: contains the class of the generic attributes, which way is better?

Time:09-17

The first way:

 public class box{
Public int=0 long;
Public int width=0;
Public int height=0;
Public object contains items=null;

What public object inside the watch ()
{
Within the return items;
}
}

Public class box & lt; T> Box:
{
Public new T=default packed commodities (T);

Public new T see inside what ()
{
Within the return items;
}
}


The second way:
 public class box 2 & lt; T> 
{
Public int=0 long;
Public int width=0;
Public int height=0;
Public T=default packed commodities (T);

Within the public T take things ()
{
Within the return items;
}
}

2: public class box box 2 & lt; Object>
{
What public object inside the watch ()
{
Within the return items;
}
}



Test:
 public class Test 
{
Public static box test_00 ()
{
Return new box & lt; Int> (a);
}

Public static box & lt; Int> Test_01 ()
{
Return new box ();
}

Public static box 2 test_10 ()
{
Return new box 2 & lt; Int> (a);
}

Public static box 2 & lt; Int> Test_11 ()
{
Return new box (2);
}
}


Test code is only the first method is correct, the three will be submitted to the type conversion of grammatical errors.

The above two kinds of implementations, box 2 should look good understand some, box 2 & lt; Object> Is the box 2 & lt; T> A special case, implemented as a subclass.
But the actual use, with the first way is better to use, more flexible type conversion.

The great god should think how to achieve better?

CodePudding user response:

The second implementation would have been better

Why

- strong typing, don't worry about the follow-up there is a parameter incoming and expectations are not the same, such as the following code

Public void a method (object) packed commodities
{
//expected within this article is type int, but actually type bool type
}

Using a strongly typed above problems will not

- improve performance when using object conversion, if it is a value type (such as int) these need to be packing and unpacking the word will find a lot of knowledge (baidu)

While using the split open a case there is a loss of performance

CodePudding user response:



There's a phrase is equal to dig a hole, flexible because too flexible, so instead of debugging difficulty

About debugging and write code related, welcome to see my blog dotnet code debugging method

CodePudding user response:

The second, generics in the base class is more suitable,
Also, you should be added for the virtual base class method
Derived classes override

CodePudding user response:

This reminds me of my usual generic way is to do so by default:
Only the Entity inherited Entity
 public static void Main (string [] args) 
{
The Entity t=new Entity ();
T.S ay ();

Entity T1=new Entity (a);
T1. Say ();

Console.ReadLine();
}

Public class Entity
{
Public T id {get; set; }

Public virtual void Say ()
{
Console. WriteLine (typeof (T). The Name).
}
}
Public class Entity: Entity
{

}

CodePudding user response:

Packing and unpacking?
Microsoft said, what have I done

The object a=1;
Int b=(int);

Even if you change form, is also a story

Xxx Push ()
Xxx The pop ()

The class xxx
T temp;
Void push Obj (T)
{
Temp=obj.
}

T pop ()
{
Return temp.
}

So, basically did not see have what effect, textual research of pure theory, the actual use value is not high
  •  Tags:  
  • C #
  • Related