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

  • Home
  • SEARCH
  • 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 7069515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:27:17+00:00 2026-05-28T05:27:17+00:00

if I use implicit operator in non generic builder class every thing is ok:

  • 0

if I use implicit operator in non generic builder class every thing is ok:

 public class ReligionBuilder
{
    private Religion _religion;

    public ReligionBuilder()
    {
        _religion = new Religion(){//some codes}

    }

    public ReligionBuilder AddToRepository()
    {
        Repository<Religion>.Add(_religion);
        return this;
    }
    public Religion Build()
    {
        return _religion;
    }

    public static implicit operator Religion(ReligionBuilder _builder)
    {
        return _builder.Build();

    }
}

I can use it :

 Religion religion=new ReligionBuilder().AddToRepository();

but if this operator is in generic class something is wrong:

 public abstract class DataTestBuilderBase<T> : IDataTestBuilder<T>
{
    protected T TestData { get; set; }

    public virtual T Build()
    {
        return TestData;
    }

    public abstract IDataTestBuilder<T> AddToRepository();
    public abstract IDataTestBuilder<T> WithDefault();

    public static implicit operator T(DataTestBuilderBase<T> builder)
    {
        return builder.Build();
    }
}


 public class PersonDataTestBuilder : DataTestBuilderBase<Person>
{
    private Person _person;

    public PersonDataTestBuilder()
    {
        //some codes
    }
    public override IDataTestBuilder<Person> AddToRepository()
    {
       //some codes
        return this;
    }
}

usage:

 PersonDataTestBuilder _testBuilder = new PersonDataTestBuilder();
        Person person = _testBuilder.AddToRepository();

the error is :cannot convert IDataTestBuilder to Person

what is the problem?

  • 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-28T05:27:18+00:00Added an answer on May 28, 2026 at 5:27 am

    AddToRepository returns just IDataTestBuilder<Person> (in terms of the compile-time return type) – and there’s no implicit conversion from that to Person. If you change the return type of the abstract method to DataTestBuilderBase<T> then it should work – although frankly I wouldn’t want to use that implicit conversion anyway. I’m generally pretty cautious about providing implicit conversions – they can often make the code less clear, as I believe they do here.

    Should you really be providing AddToRepository on a builder anyway? It feels like an inappropriate action for a builder – I would expect:

    Person person = new PersonBuilder { /* properties */ }
                         .Build()
                         .AddToRepository();
    

    EDIT: Just to show what I mean about changing the return type of AddToRepository, here’s a short but complete program to demonstrate. It works fine.

    using System;
    
    public abstract class BuilderBase<T>
    {
        public abstract T Build();
        public abstract BuilderBase<T> AddToRepository();
    
        public static implicit operator T(BuilderBase<T> builder)
        {
            return builder.Build();
        }
    }
    
    public class TestBuilder : BuilderBase<string>
    {
        public override string Build()
        {
            return "Built by Build()";
        }
    
        public override BuilderBase<string> AddToRepository()
        {
            return this;
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            string x = new TestBuilder().AddToRepository();
            Console.WriteLine(x);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function public static implicit operator MyClass(string v) { return new MyClass(v);
I defined two classes: class Token_ { public: virtual char operator*()const = 0;//this fnc
I know that I can use implicit conversions with a class as follows but
I have a generic class that I'm trying to implement implicit type casting for.
When is it safe to use implicit casting? Use Case : I'm working with
I used to use the implicit call of toString when wanting some debug info
In C#, you can use the implicit keyword to define an implicit user-defined type
I am trying to use the conditional operator, but I am getting hung up
I have this very simple wrapper template: template<class T> struct wrapper { inline operator
I have this custom type: public struct PasswordString { private string value; public PasswordString(string

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.