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 6661043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:13:49+00:00 2026-05-26T02:13:49+00:00

I have a util class (C#) where I have a static method that takes

  • 0

I have a util class (C#) where I have a static method that takes a certain type of object and brings up a web service to get further data. I would like to support other object types and to not replicate the code, i.e. add multiple similar methods, I am thinking the Generic route would be best.

For example, let’s say I have:

public static void GetData(Building building)
{
    var webClient = new WebClient();
    var wrapper = new WrapperClass(building);

    if (building.Distance.HasValue)
    {
        structure = new Structure((decimal)building.Length.Value, (decimal)building.Height.Value);
    }

    ... // and so on ...

instead of creating another method(s) like so:

public static void GetDataForBridge(Bridge bridge)
{
    var webClient = new WebClient();
    var wrapper = new WrapperClass(bridge);

    if (bridge.Distance.HasValue)
    {
        structure = new Structure((decimal)bridge.Length.Value, (decimal)bridge.Height.Value);
    }

    // ...

I am not sure how to do this using Generics. Can anyone please give me some tips or advice?

  • 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-26T02:13:50+00:00Added an answer on May 26, 2026 at 2:13 am

    In this case, Bridge and Building would have to implement the same interface, say, IObjectWithHeightAndWidth. You’d then specify this interface as a constraint on the type parameter of your generic method.

    (Or, instead of a common interface, the classes could share a common base class.)

    As other posters have pointed out, though, you might not need generics at all. You only would need generics if you subsequently need to have a strongly-typed reference to the object as a Bridge or Building — for example if you need to call another generic method in the method we’re discussing.

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

Sidebar

Related Questions

I have a static util class that does some string manipulation on a bit
In Java, the java.util.Arrays class have several static toString(...) methods that take an array
Let's say I have a java.util.Properties object. The Properties object has a method called
Assume, that I have class with static methods only. Will class loader load every
I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
In my spring application context file, I have something like: <util:map id="someMap" map-class="java.util.HashMap" key-type="java.lang.String"
On a JSTL/JSP page, I have a java.util.Date object from my application. I need
I am trying to create a method that pretty much takes anything as a
I created a class Foo that has the method toArray() that returns an Array<Int>

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.