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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:27:03+00:00 2026-06-02T01:27:03+00:00

I would like to initialize an object of type B from another class A

  • 0

I would like to initialize an object of type B from another class A, why I still get null? Is it possible to do it without using ref and out modifiers?

    class A
    {
        public void Initialize(B value)
        {
            if (value == null)
            {
                value = new B();
            }
        }
    }

    class B
    {

    }

    class Program
    {
        static void Main(string[] args)
        {
            A a = new A();
            B b = null;
            a.Initialize(b);
        }
    }

[upd.] I thought the b variable could be passed by ref because of it’s the instance of the class.

  • 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-06-02T01:27:06+00:00Added an answer on June 2, 2026 at 1:27 am

    It is possible, just make Initialize() a function:

    class A
    {
        public B Initialize(B value)
        {
            if (value == null)
            {
                value = new B();
            }
            return value;
        }
    }
    

    And call it like:

        static void Main(string[] args)
        {
            A a = new A();
            B b = null;
            b = a.Initialize(b);
        }
    

    This is a good solution, the data flow is clearly visible. No surprises.

    But otherwise, just use a ref (not an out) : public void Initialize(ref B value) and call a.Initialize(ref b);


    I thought the b variable could be passed by ref because of it’s the instance of the class.

    To answer that you need very precise wording: b is not the instance of a class. b is a reference to an instance. Instances never have a name.

    And b is treated just like a value type: the reference is passed by value to the method. Unless you use ref.

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

Sidebar

Related Questions

I would like to know: how can i initialize class object with parameters? I've
I would like to initialize boost::random::discrete_distribution with a double[] like this: boost::random::discrete_distribution<>* distribution(double* _distr)
I would like to define and initialize some variables in web.xml and the access
Would like to know the c# code to actually retrieve the IP type: Static
I've got an object 'Foo Action' which is of type FooProcess (interface) [null object].
I'm writing a template class and at one point in my code would like
I'm coming from a C# background and I really like the type inference that
I would like to initialize a select with an initial value. I have a
I would like to have a dictionary object contains string keys and values, which
I would like to know if it's possible to init a subclass of NSManagedObject

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.