none
Is there any problem when generic type parameter and non-generic type parameter are both used in a function? RRS feed

  • Question

  • Dear All,

    https://stackoverflow.com/questions/36071994/how-can-i-serialize-a-list-of-classes-that-ive-created

    I found a code snippet from website,

    Is there any problem if the parameter is extended to generic type parameter and none-generic type parameter, like below,

    From document below, it seems no body use  generic type parameter and non-generic type parameter, 

    https://docs.microsoft.com/zh-tw/dotnet/csharp/programming-guide/generics/generic-type-parameters

    Thanks and Best regards,

    E-John

            public void Serialize<T>(T data, string fileName)
            {
                // Use a file stream here.
                using (TextWriter WriteFileStream = new StreamWriter(fileName))
                {
                    // Construct a SoapFormatter and use it  
                    // to serialize the data to the stream.
                    XmlSerializer SerializerObj = new XmlSerializer(typeof(T));
    
                    try
                    {
                        // Serialize EmployeeList to the file stream
                        SerializerObj.Serialize(WriteFileStream, data);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(string.Format("Failed to serialize. Reason: {0}", ex.Message));
                    }
                }
            }
    
            public T Deserialize<T>(string fileName) where T : new()
            {
                //List<Employee> EmployeeList2 = new List<Employee>();
                // Create an instance of T
                T ReturnListOfT = CreateInstance<T>();
    
                // Create a new file stream for reading the XML file
                using (FileStream ReadFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    // Construct a XmlSerializer and use it  
                    // to serialize the data from the stream.
                    XmlSerializer SerializerObj = new XmlSerializer(typeof(T));
                    try
                    {
                        // Deserialize the hashtable from the file
                        ReturnListOfT = (T)SerializerObj.Deserialize(ReadFileStream);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(string.Format("Failed to serialize. Reason: {0}", ex.Message));
                    }
    
                }
                // return the Deserialized data.
                return ReturnListOfT;
            }
    
            // function to create instance of T
            public static T CreateInstance<T>() where T : new()
            {
                return (T)Activator.CreateInstance(typeof(T));
            }


    • Edited by E-John Wednesday, November 28, 2018 8:29 AM
    Wednesday, November 28, 2018 8:25 AM

Answers

  • There is nothing wrong with mixing generic types and regular types. We do it all the time and the framework is littered with examples.

    Also, your CreateInstance static method doesn't need to use Activator. By specifying the new() constraint you can simply use the new operator.

    static T CreateInstance<T> () where T: new()
    {
        return new T();
    }


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, November 28, 2018 2:59 PM
    Moderator

All replies

  • Why would you want to use non generic type parameter ? Can you elaborate more?

    In Non-generic it is for particular type implementation and will only take input parameter of that specific type while in generic type parameter implementation we have the flexibility to pass any type when calling the method.


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites


    • Edited by Ehsan Sajjad Wednesday, November 28, 2018 8:38 AM
    Wednesday, November 28, 2018 8:36 AM
  • There is nothing wrong with mixing generic types and regular types. We do it all the time and the framework is littered with examples.

    Also, your CreateInstance static method doesn't need to use Activator. By specifying the new() constraint you can simply use the new operator.

    static T CreateInstance<T> () where T: new()
    {
        return new T();
    }


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, November 28, 2018 2:59 PM
    Moderator
  • Hi CoolDadTx,

    Got it. Thanks a lot.

    Best regards,

    E-John

    Tuesday, December 4, 2018 2:25 AM