Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

explicit and implicit c#

I'm new to C# and learning new words. I find it difficult to understand what's the meaning of these two words when it comes to programming c#. I looked in the dictionary for the meaning and here's what I got:

Implicit

"Something that is implicit is expressed in an indirect way."

"If a quality or element is implicit in something, it is involved in it or is shown by it;"

Explicit

"Something that is explicit is expressed or shown clearly and openly, without any attempt to hide anything"

"If you are explicit about something, you speak about it very openly and clearly."

I would like to understand it in C#.

Thanks for your help.

Cheers


additional info:

Here is a part of sentence in the book what I'm reading now which has this word "implicit"

"This means that Area and Occupants inside AreaPerPerson( ) implicitly refer to the copies of those variables found in the object that invokes AreaPerPerson( )"

I quite don't understand what this sentence here trying to say.

like image 976
tintincutes Avatar asked Jul 24 '09 09:07

tintincutes


People also ask

What is the difference between implicit and explicit type conversion in C?

Implicit conversion is the conversion in which a derived class is converted into a base class like int into a float type. Explicit conversion is the conversion that may cause data loss. Explicit conversion converts the base class into the derived class.

What is explicit and implicit?

Explicit describes something that is very clear and without vagueness or ambiguity. Implicit often functions as the opposite, referring to something that is understood, but not described clearly or directly, and often using implication or assumption.

What is implicit type casting and explicit type casting in C?

There are two type of type conversion: implicit and explicit type conversion in C. Implicit type conversion operates automatically when the compatible data type is found. Explicit type conversion requires a type casting operator.

Does C have implicit conversion?

Implicit type conversion in C language is the conversion of one data type into another datatype by the compiler during the execution of the program. It is also called automatic type conversion.


2 Answers

The implicit and explicit keywords in C# are used when declaring conversion operators. Let's say that you have the following class:

public class Role {     public string Name { get; set; } } 

If you want to create a new Role and assign a Name to it, you will typically do it like this:

Role role = new Role(); role.Name = "RoleName"; 

Since it has only one property, it would perhaps be convenient if we could instead do it like this:

Role role = "RoleName"; 

This means that we want to implicitly convert a string to a Role (since there is no specific cast involved in the code). To achieve this, we add an implicit conversion operator:

public static implicit operator Role(string roleName) {     return new Role() { Name = roleName }; } 

Another option is to implement an explicit conversion operator:

public static explicit operator Role(string roleName) {     return new Role() { Name = roleName }; } 

In this case, we cannot implicitly convert a string to a Role, but we need to cast it in our code:

Role r = (Role)"RoleName"; 
like image 83
Fredrik Mörk Avatar answered Sep 20 '22 14:09

Fredrik Mörk


In general

  • Implicit: something is being done for you automatically.
  • Explicit: you've written something in the source code to indicate what you want to happen.

For example:

int x = 10; long y = x; // Implicit conversion from int to long int z = (int) y; // Explicit conversion from long to int 

Implicit and explicit are used quite a lot in different contexts, but the general meaning will always be along those lines.

Note that occasionally the two can come together. For instance:

int x = 10; long y = (long) x; // Explicit use of implicit conversion! 

(An explicit conversion is one which has to be stated explicitly; an implicit version is one which can be used implicitly, i.e. without the code having to state it.)

like image 26
Jon Skeet Avatar answered Sep 19 '22 14:09

Jon Skeet