Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5993249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:38:53+00:00 2026-05-22T23:38:53+00:00

I am starting to use generics and been unable to really find the simple

  • 0

I am starting to use generics and been unable to really find the simple beginners guide to them yet so I am just doing trial and error.

I want to convert this

public void CreateTask(Task task, Student student)
{
            Task convertToUtcTime = task.ConvertToUtcTime(student);
            session.Save(convertToUtcTime);
}


public static Task ConvertToUtcTime(this Task task, Student student)
{
    if (student != null)
    {
        TimeZoneInfo info = TimeZoneInfo.FindSystemTimeZoneById(student.TimeZoneId);
        task.DueDate = TimeZoneInfo.ConvertTimeToUtc(task.DueDate, info);
    }

    return task;
}

to be generic

I started trying this(I have no complied it yet so this might not even work)

  public void Create<T>(T entity, Student student)
    {
                T convertToUtcTime = entity.ConvertToUtcTime(student);
                session.Save(convertToUtcTime);
    }


  public static T ConvertToUtcTime(this T entity, Student student)
    {
        if (student != null)
        {
            TimeZoneInfo info = TimeZoneInfo.FindSystemTimeZoneById(student.TimeZoneId);
            entity.DueDate = TimeZoneInfo.ConvertTimeToUtc(entity.DueDate, info);
        }

        return entity;
    }

Now what confuses the heck out of me is how do I cast “entity” to a Task object but now only that but other objects that I need to convert like an Appointment that needs a timezone.

I just can’t figure out how I could make it convert different timeszones for all my different objects.

Edit 2

// also used with nhibernate hence why everything is virtual
    public class Task :IEntity
{
    public virtual int TaskId { get; private set; }
    public virtual DateTime DueDate { get; set; }


    public virtual Task ConvertToUtcTime2(Student student)
    {
        DateTime s = DueDate ;
        // not use to returning "this" but seems only way how to get the object back.
        // I also realized I can do this as well 
        this.ConvertToUtcTime(Student); // So I am still using my extension method and no need to duplicate that code.
        return this;
    }
}

 public interface IEntity
    {
        IEntity ConvertToUtcTime2(Student student);
        // more methods/properties
    }

  public void Create<T>(T entity, Student student) where T: IEntity
        {
            entity.ConvertToUtcTime2(student);
        }

   // call generic method.
   nhibernateRepo.Create(task, student);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:38:54+00:00Added an answer on May 22, 2026 at 11:38 pm

    Now what confuses the heck out of me is how do I cast “entity” to a Task object but now only that but other objects that I need to convert like an Appointment that needs a timezone.

    You DO NOT need to cast if you are using generics. Or put this in another way: the reason you are using generics is because you do not want to cast. To illustrate this, below is an excerpt from An Introduction to C# Generics of MSDN.

    Because the generic code does not force the boxing and unboxing of value types, or the down casting of reference types, performance is greatly improved.

    In general, generics has the following benefits:

    • improves performance
    • enforces type-safety
    • reusable

    If you need a more specific example, look at (and compare) ArrayList and List(T) (search for them in MSDN). For the add method, ArrayList has the following signature:

    public virtual int Add(Object value)

    The same method of List(T) is defined as:

    public void Add(T item)

    Thus, you can see that a generic method takes in ANY type (reusable), and works on the type in a type-safe manner without the need to cast (improves performance). Of course, ArrayList is also reusable, but you need to do casting on it, making it less type-safe and has worse performance.

    Having said that, my previous paragraph actually has a confusion. While you do can make use of generic to work on ANY type, you can also implement a constraint to have the generic parameter implementing a particular interface, thus limiting the generic parameter to a certain set of classes (those that implement the interface). A more in-depth look at the topic (Generic Constraints) can be found in An Introduction to C# Generics mentioned previously.

    As for code sample, @Ed S. has actually showed a viable way. (Referring to the code sample of @Ed S.) Through the use of generic constraints, the parameter is thus limited to classes that implement IEntity, an interface of which has a ConvertToUtcTime method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just starting with ASP.NET and find it difficult to use the GridView. I have
I'm just starting to use Hibernate and I was wondering how to insert a
I am just starting to use Wix and would like to use heat to
i'm just starting to use asp.net mvc.. since i'm not familiar with it, i
I'm just starting to work on a logging library that everyone can use to
I've been starting to use Spring's @Transactional annotation, and it provides a lot of
I'm just starting learning WPF and I'm trying to use a GridViewRowPresenter inside of
I am starting to use mongodb for data storage. In mysql I save a
I am starting to use some Java code which was written by someone else.
I'm starting to use Moq and I cannot figure out how to test the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.