Initialize own type with string

Cubi73

Is it possible to instantiate a variable like following example

OwnType variable = "String";

instead of

OwnType variable = new OwnType("String");

Maybe with a index field in OwnType or something similar?

Sergey Kalinichenko

Yes, you can do it with an implicit conversion operator from string to OwnType. This would let you hide the call of operator new in the operator:

public static implicit operator OwnType(string str)
{
    return new OwnType(str);
}

Demo on ideone.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Initialize own type with string

From Dev

Initialize classes in their own interface

From Dev

Java - From a String Type name how to initialize an ArrayList of that Type

From Dev

cannot be used to initialize an entity of type const std::string *

From Dev

Thread Synchronisation : Threads having their own copy of string type lock

From Dev

How to use Autofac to initialize your own components?

From Dev

Cannot initialize type 'HttpClientHandler'

From Dev

Haskell: No instance for own type

From Dev

how to initialize a string to a date-time field type in SharePoint using c#

From Dev

initialize string dynamically in java

From Dev

Initialize a string object in a struct

From Dev

How to initialize a string?

From Dev

Initialize a string object in a struct

From Dev

Initialize String in C++

From Dev

HKQuantityTypeIdentifier initialize with string

From Dev

cannot initialize type with a collection initializer

From Dev

How to properly initialize a var type

From Dev

Initialize ArrayList of Type Abstract class

From Dev

initialize object type constructor oracle

From Dev

Java: Reference "own type" in interface

From Dev

Create my own type in Swagger

From Dev

Adding two lists of own type

From Dev

Create my own type in Swagger

From Dev

Java: Reference "own type" in interface

From Dev

how can I list-initialize my own class?

From Dev

How to specify "own type" as return type in Kotlin

From Dev

How to Initialize NSTextStorage with a String in Swift

From Java

Declare and Initialize String Array in VBA

From Dev

Initialize Array with string in array form