• Blog
  • Info Support
  • Career
  • Training
  • International Group
  • Info Support
  • Blog
  • Career
  • Training
  • International Group
  • Search
logo InfoSupport
  • Latest blogs
  • Popular blogs
  • Experts
      • All
      • Bloggers
      • Speakers
  • Meet us
  • About us
    • nl
    • en
    • .NET
    • 3D printing
    • Advanced Analytics
    • Agile
    • Akka
    • Alexa
    • Algorithms
    • Api's
    • Architectuur
    • Artificial Intelligence
    • ATDD
    • Augmented Reality
    • AWS
    • Azure
    • Big Data
    • Blockchain
    • Business Intelligence
    • Chatbots
    • Cloud
    • Code Combat
    • Cognitive Services
    • Communicatie
    • Containers
    • Continuous Delivery
    • CQRS
    • Cyber Security
    • Dapr
    • Data
    • Data & Analystics
    • Data Science
    • Data Warehousing
    • Databricks
    • DataOps
    • Developers life
    • DevOps
    • Digital Days
    • Digital Twin
    • Docker
    • eHealth
    • Enterprise Architecture
    • Event Sourcing
    • Hacking
    • Infrastructure & Hosting
    • Innovatie
    • Integration
    • Internet of Things
    • Java
    • Machine Learning
    • Microservices
    • Microsoft
    • Microsoft Bot Framework
    • Microsoft Data Platform
    • Mobile Development
    • Mutation Testing
    • Open source
    • Pepper
    • Power BI
    • Privacy & Ethiek
    • Python
    • Quality Assistance & Test
    • Quality Assurance & Test
    • Requirements Management
    • Scala
    • Scratch
    • Security
    • SharePoint
    • Software Architecture
    • Software development
    • Software Factory
    • SQL Server
    • SSL
    • Start-up
    • Startup thinking
    • Stryker
    • Test Quality
    • Testing
    • TLS
    • TypeScript
    • Various
    • Web Development
    • Web-scale IT
    • Xamarin
    • All
    • Bloggers
    • Speakers
Home » Generics
  • Generics

    • By Erno de Weerd
    • Various 17 years ago
    • Various 0 comments
    • Various Various
    Generics

    The new versions of .NET (2.0) and Java (1.5) provide a new construct called Generics. I have been testing both and here is some code that I wrote to test and compare.


    .NET:

    using System;
    using System.Collections.Generic;
    using System.Text;

    class linkedListNode
    {
    private linkedListNode next = null;
    private NodeType value;

    public linkedListNode(NodeType value)
    {
    this.value = value;
    }

    public void link(linkedListNode element)
    {
    if (next != null)
    {
    next.link(element);
    }
    else
    {
    next = element;
    }
    }

    public void print()
    {
    if (value != null)
    {
    System.Console.WriteLine(value.ToString());
    }
    else
    {
    System.Console.WriteLine(“Empty node.”);
    }

    if (next == null)
    {
    System.Console.WriteLine(“Last node.”);
    }
    else
    {
    next.print();
    }
    }
    }

    class linkedList
    {
    private linkedListNode root = null;

    public void add(NodeType element)
    {
    linkedListNode node = new linkedListNode(element);

    if (root != null)
    {
    root.link(node);
    }
    else
    {
    root = node;
    }
    }

    public void print()
    {
    System.Console.WriteLine(“The list is a list of ” + root.GetType().ToString());
    root.print();
    }
    }

    class thingy
    {
    private String text;

    public thingy(String text)
    {
    this.text = text;
    }

    public override String ToString()
    {
    return text;
    }
    }

    public class test
    {
    public static void Main()
    {
    linkedList list = new linkedList();
    thingy t;
    t = new thingy(“t1”);
    list.add(t);
    t = new thingy(“t2”);
    list.add(t);
    list.print();
    System.Console.ReadLine();
    }
    }

    Java:
    class linkedListNode<NodeType>
    {
    private linkedListNode next = null;
    private NodeType value;

    public linkedListNode(NodeType value)
    {
    this.value = value;
    }

    public void link(linkedListNode<NodeType> element)
    {
    if(next != null)
    {
    next.link(element);
    }
    else
    {
    next = element;
    }
    }

    public void print()
    {
    if(value != null)
    {
    System.out.println(value.toString());
    }
    else
    {
    System.out.println(“Empty node.”);
    }

    if(next == null)
    {
    System.out.println(“Last node.”);
    }
    else
    {
    next.print();
    }
    }
    }

    class linkedList<NodeType>
    {
    private linkedListNode root = null;

    public void add(NodeType element)
    {
    linkedListNode<NodeType> node = new linkedListNode<NodeType>(element);
    if(root != null)
    {
    root.link(node);
    }
    else
    {
    root = node;
    }
    }

    public void print()
    {
    System.out.println(“The list is a list of ” + root.getClass().getName());
    root.print();
    }
    }

    class thingy
    {
    private String text;

    public thingy(String text)
    {
    this.text = text;
    }

    public String toString()
    {
    return text;
    }
    }

    public class test
    {
    public static void main(String[] args)
    {
    linkedList<thingy> list = new linkedList<thingy>();
    thingy t;
    t = new thingy(“t1”);
    list.add(t);
    t = new thingy(“t2”);
    list.add(t);
    list.print();
    }
    }


    Some interesting points:



    • C# syntax almost equal to Java syntax
    • The output is different; Java loses type information
    • The lost type information is also visible when compiling.
    Read this excellent document on Generics in Java and .NET.

    Share this

Erno de Weerd

View profile

IT Training at Info Support

Which training fits you?

Consultancy

Consultancy

Related blogs

  • Video Conferencing en OBS Studio koppelen: online prese…

    Video Conferencing en OBS Studio koppelen: online prese… Maaike Brouwer - 2 years ago

  • Verantwoordelijkheid pakken in jouw eigen persoonlijke …

    Verantwoordelijkheid pakken in jouw eigen persoonlijke … Stephan Versteegh - 2 years ago

  • Tips voor als je gaat afstuderen

    Tips voor als je gaat afstuderen Bart Renders - 2 years ago

Data Discovery Channel

  • Modern Data Platform

  • Gartner Data & Analytics Summit 2022

  • De Data Architecture ®Evolution

Nieuwsbrief

* verplichte velden

Contact

  • Head office NL
  • Kruisboog 42
  • 3905 TG Veenendaal
  • T +31 318 552020
  • Call
  • Mail
  • Directions
  • Head office BE
  • Generaal De Wittelaan 17
  • bus 30 2800 Mechelen
  • T +32 15 286370
  • Call
  • Mail
  • Directions

Follow us

  • Twitter
  • Facebook
  • Linkedin
  • Youtube

Newsletter

Sign in

Extra

  • Media Library
  • Disclaimer
  • Algemene voorwaarden
  • ISHBS Webmail
  • Extranet
Beheer cookie toestemming
Deze website maakt gebruik van Functionele en Analytische cookies voor website optimalisatie en statistieken.
Functioneel Always active
De technische opslag of toegang is strikt noodzakelijk voor het legitieme doel het gebruik mogelijk te maken van een specifieke dienst waarom de abonnee of gebruiker uitdrukkelijk heeft gevraagd, of met als enig doel de uitvoering van de transmissie van een communicatie over een elektronisch communicatienetwerk.
Voorkeuren
De technische opslag of toegang is noodzakelijk voor het legitieme doel voorkeuren op te slaan die niet door de abonnee of gebruiker zijn aangevraagd.
Statistieken
De technische opslag of toegang die uitsluitend voor statistische doeleinden wordt gebruikt. De technische opslag of toegang die uitsluitend wordt gebruikt voor anonieme statistische doeleinden. Zonder dagvaarding, vrijwillige naleving door uw Internet Service Provider, of aanvullende gegevens van een derde partij, kan informatie die alleen voor dit doel wordt opgeslagen of opgehaald gewoonlijk niet worden gebruikt om je te identificeren.
Marketing
De technische opslag of toegang is nodig om gebruikersprofielen op te stellen voor het verzenden van reclame, of om de gebruiker op een website of over verschillende websites te volgen voor soortgelijke marketingdoeleinden.
Manage options Manage services Manage vendors Read more about these purposes
Voorkeuren
{title} {title} {title}