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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:31:53+00:00 2026-05-19T01:31:53+00:00

Given some code like this: class Json { } class Program { static void

  • 0

Given some code like this:

class Json
{
}

class Program
{
    static void Main(string[] args)
    {
    }

    Json MyAction()
    {
        return "{\"json\": 1}";
    }
}

Is there anything I can add to the Json class to make this compile? Something to let the compiler know that it can implicitly cast a string to a Json?

  • 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-19T01:31:53+00:00Added an answer on May 19, 2026 at 1:31 am

    The implicit operator declaration in c# allows you to specify:

    {type to convert to} ({type to convert from} variablename)
    

    Here’s a simple example:

    class Json
    {
        private Json(string json)
        {
            //logic to parse string into object
        }
    
        public static implicit operator Json(string input)
        {
            return new Json(input);
        }
    }
    

    Some notes:

    • First, I wouldn’t necessarily go this route, since most strings in your application are not semantically equivalent to a JSON string. The purpose of a conversion operator is to say two types always represent semantically equivalent information (or close enough to semantically equivalent to be usefully treated as such). I’d recommend implementing a static Json Parse(string input) or maybe even a static bool TryParse(string input, out Json json) method on your Json class instead. The callsite, generally, ought to know whether it expects its own string to contain Json.

    • Typically if my class offers implicit type conversion from a type, I find it best to make any parsing or ctor logic for that same type private. This ensures there’s only one way for consumers to do a particular thing, not two ways (ctor and conversion) – hence the private ctor in the example above. There can be exceptions to this but it’s been a good general rule for me.

    • The implicit conversion also allows some interesting things with comparison operators. For example, now that you can implicitly convert from string to json, you can also do: if(myJson == "blah") and it will do the conversion and then call the == operator on your Json object, which by default will do a reference equality comparison.

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

Sidebar

Related Questions

Given some JS code like that one here: for (var i = 0; i
I've been given some code with commenting unlike anything I've come across before: //{{{
Can someone give me some example code that creates a surface with a transparent
Could anyone could point me to some code/give me ideas on how to create
I have some code that gives a user id to a utility that then
Why do some sites (or advertisers that give clients javascript code) employ a technique
I was looking into sorting tables by a column designated given some input, and
Given the key for some registry value (e.g. HKEY_LOCAL_MACHINE\blah\blah\blah\foo) how can I: Safely determine
Given a pointer to some variable.. is there a way to check whether it
Given a bitmap image with some blots of solid color on it, what algorithm

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.