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

The Archive Base Latest Questions

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

Instead of writing int i = new int(); i = 7; One can write

  • 0

Instead of writing

 int i = new int();
 i = 7; 

One can write

int i = 7; 

Is there a way I can get that style of initialisation for my own types?

MyType mt = xyz; 
  • 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-26T23:13:06+00:00Added an answer on May 26, 2026 at 11:13 pm

    The closest you can come is by creating implicit conversions on your type. For example:

    public class Unit
    {
      public static implicit operator Unit( string val )
      {
         return Unit.Parse( val );
      }
    
      public static Unit Parse( string unitString )
      {
        // parsing magic goes here
      }
    }
    

    This would enable you to do something like this:

    Unit width = "150px";
    var width = Unit.Parse("150px"); // equivalent to previous line
    

    Note that you cannot introduce new syntax; this would be impossible to implement:

    Unit width = 150px;
    

    since 150px does not represent a valid value type.

    Note that implicit casting can get you into trouble in weird ways so don’t over do it. Support implicit casting only to and from types that you’re really going to need.

    Alternatively, if you’re using C# compiler 3.5 or up you can also use inline initialization which is more verbose but also more explicit:

    Unit with = new { Value=150, UnitType=Units.Pixel };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a program I'm writing, I've decided to use my own data types instead
When writing the string ¿ out using System.out.println(new String(¿.getBytes(UTF-8))); ¿ is written instead of
I'm writing a bookmarklet, i.e. a bookmark that contains javascript instead of a URL,
In a Silverlight application, instead of consuming and writing (wcf) wrappers around messages that
In C, is there a difference between writing struct foo instead of just foo
FileReader rd=new FileReader(new.mp4); FileWriter wr=new FileWriter(output.mp4); int ch; while((ch=rd.read())!=-1) wr.write(ch); wr.flush(); wr.close(); When I
Instead of writing my ASP.NET C# applications in Visual Studio, I used my favorite
Instead of writing ((x: Double) => (((y: Double) => y*y))(x+x))(3) I would like to
I'm just getting started on writing functions instead of writing everything inline. Is this
OK, so instead of writing a whole bunch of access control specs, and duplicating

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.