Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Shoe-horning generics into a project just because "I want to use generics" is usually a bad idea. use the right tool for the right job. now, props for trying to learn something new.</p> <p>that said... Basically, a "generic" is a way of specifying a method, class (etc) without specifying an underlying type when you write it. It is a good way to separate your algorithm from you data type.</p> <p>take, for example, a Swap method. Basically, the swap algorithm is the same no matter what the type it is operating on. So, this would be a good candidate for a generic (as would a <code>List</code>, a <code>Dictionary</code>, etc)</p> <p>so, a swap for an <code>int</code> would like like this:</p> <pre><code>void Swap(ref int left, ref int right) { int temp = left; left = right; right = temp; } </code></pre> <p>now, you COULD write overloads for your other datatypes (<code>float</code>, <code>double</code>, etc) or you COULD make it a generic and write it once so it will work on pretty much all datatypes:</p> <pre><code>void Swap&lt;_type&gt;(ref _type left, ref _type right) { _type temp = left; left = right; right = temp; } </code></pre> <p>now, your sample code wont work:</p> <pre><code>Public void CreateAccount(int duration, int amount) { T.DurationInMonth = duration; T.DepositedAmount = amont; int interestVal = T.Interest; SaveToDatabase (T); } </code></pre> <p>here, <code>T</code> is the type, not an instance of an object. if you substitute for <code>T</code>, it becomes clearer what is going on:</p> <pre><code>Public void CreateAccount(int duration, int amount) { IbankAccount.DurationInMonth = duration; IbankAccount.DepositedAmount = amont; int interestVal = IbankAccount.Interest; SaveToDatabase (IbankAccount); } </code></pre> <p>when what you REALLY want is this:</p> <pre><code>Public void CreateAccount(int duration, int amount) { account.DurationInMonth = duration; account.DepositedAmount = amont; int interestVal = account.Interest; SaveToDatabase (account); } </code></pre> <p>you see, here we are calling the methods of the INSTANCE of the class account, not of the generic <code>IbankAccount</code> TYPE</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload