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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:21:05+00:00 2026-05-26T00:21:05+00:00

I am trying to return an object of a class using the generics. This

  • 0

I am trying to return an object of a class using the generics.

This is the generic class

public class ClientBase <S>
{
    protected S CreateObject()
    {
        return default(S)  ;
    }
}

This is how I am trying to use it…

public class ClientUser : ClientBase <SomeClass>
{

    public void call()
    {
        var client = this.CreateObject();
        client.SomeClassMethod();
     }
}

While I get the SomeClassMethod() in the client object, when running the code it gives an error at the line:

client.SomeClassMethod();

Error is ‘Object reference not set to an instance of an object’. I know there is something missing in the generic class ClientBase’s CreateObject() method; just cant figure that bit out. Could someone help me here please?

Thanks for your time…

  • 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-26T00:21:05+00:00Added an answer on May 26, 2026 at 12:21 am

    default(S) where S is a reference type is null. In your case, default(SomeClass) returns null. When you try to invoke a method on a null reference, that’s when you get your exception.

    Are you trying to return a default instance of SomeClass? You may want to use a new() constraint and return new S() in your generic class instead, like so:

    public class ClientBase<S> where S : new()
    {
        protected S CreateObject()
        {
            return new S();
        }
    }
    

    If S needs to be a reference type you can also constrain it to class:

    public class ClientBase<S> where S : class, new()
    {
        protected S CreateObject()
        {
            return new S();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to return a JSON object from an aspx page using Jayrock.JSON.
i'm trying to return a custome object from a web service object returned only
I'm trying to return a reference to an object, not pass it by value.
I'm trying to return a double from another object then store that into a
I'm trying to create a UniqueAttribute using the System.ComponentModel.DataAnnotations.ValidationAttribute I want this to be
I am trying to create a generic method using EF4 to find the primary
I am trying to create a generic callback object that will hold arbitrary data
I am trying to pass an object of model class for edit but receing
I'm trying to implement a helper method using generics (C# / 3.5) I've a
This is what I have so far using System; using System.Collections.Generic; using System.Data.Linq; using

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.