Demonstrate a generic method : Generic Method « Generics « C# / C Sharp

C# / C Sharp
1. 2D Graphics
2. Class Interface
3. Collections Data Structure
4. Components
5. Data Types
6. Database ADO.net
7. Design Patterns
8. Development Class
9. Event
10. File Stream
11. Generics
12. GUI Windows Form
13. Language Basics
14. LINQ
15. Network
16. Office
17. Reflection
18. Regular Expressions
19. Security
20. Services Event
21. Thread
22. Web Services
23. Windows
24. XML
25. XML LINQ
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C# / C Sharp » Generics » Generic MethodScreenshots 
Demonstrate a generic method
Demonstrate a generic method


using System;

class ArrayUtils {

  public static bool copyInsert<T>(T e, int idx, T[] src, T[] target) {

    if(target.Length < src.Length+1)
      return false;

    for(int i=0, j=0; i < src.Length; i++, j++) {
      if(i == idx) {
        target[j= e;
        j++;
      }
      target[j= src[i];
    }

    return true;
  }
}

class Test {
  public static void Main() {
    int[] nums = 12};
    int[] nums2 = new int[4];

    Console.Write("Contents of nums: ");
    foreach(int x in nums)
      Console.Write(x + " ");

    Console.WriteLine();

    ArrayUtils.copyInsert(992, nums, nums2);

    Console.Write("Contents of nums2: ");
    foreach(int x in nums2)
      Console.Write(x + " ");

    Console.WriteLine();

    string[] strs = "Generics""are""powerful."};
    string[] strs2 = new string[4];

    Console.Write("Contents of strs: ");
    foreach(string s in strs)
      Console.Write(s + " ");

    Console.WriteLine();

    ArrayUtils.copyInsert("in C#"1, strs, strs2);

    Console.Write("Contents of strs2: ");
    foreach(string s in strs2)
      Console.Write(s + " ");

  }
}
           
       
Related examples in the same category
1. This is a prototypical generic method:
2. A prototypical generic methodA prototypical generic method
3. Generic methods can overload nongeneric methodsGeneric methods can overload nongeneric methods
w___w_w__.___j__av___a___2_s.c___o__m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.