---
title: "What does the explicit keyword mean?"  
description: "What does the explicit keyword mean?"  
author: "Utpal Vishwas"  
published: 2023-07-07  
updated: 2023-07-10  
canonical: https://www.mindstick.com/forum/158989/what-does-the-explicit-keyword-mean  
category: "c#"  
tags: ["c#", "c++", "programming language"]  
reading_time: 3 minutes  

---

# What does the explicit keyword mean?

What does the [explicit](https://www.mindstick.com/interview/2677/can-you-explain-about-implicit-and-explicit-type-casting) [keyword mean](https://www.mindstick.com/interview/2734/what-does-the-static-keyword-mean-can-you-override-private-or-static-method-in-java)?

## Replies

### Reply by Aryan Kumar

In C++, the `explicit` [keyword](https://www.mindstick.com/forum/33572/sql-inner-join-keyword) is used to prevent implicit conversions from one type to another. For example, the following code will compile without any errors:

C++

```plaintext
int main() {
  int x = 10;
  float y = x; // This is an implicit conversion
  return 0;
}
```

In this code, the integer `x` is implicitly converted to a float `y`. This is because the `float` type is considered to be "larger" than the `int` type.

However, if we use the `explicit` keyword, the implicit conversion will not be allowed:

C++

```plaintext
int main() {
  int x = 10;
  explicit float y = x; // This will not compile
  return 0;
}
```

In this code, the compiler will generate an error because the `explicit` keyword is used. This means that the conversion from `int` to `float` must be explicit.

The `explicit` keyword can be used with constructors, casts, and type conversion operators.

Here are some examples of how the `explicit` keyword can be used:

- **Constructors:** The `explicit` keyword can be used with constructors to prevent implicit conversions from one type to another. For example, the following code will not compile:

C++

```plaintext
class MyClass {
  explicit MyClass(int x) {}
};

int main() {
  MyClass my_class(10); // This will not compile
  return 0;
}
```

In this code, the `MyClass` constructor is declared with the `explicit` keyword. This means that the constructor cannot be called implicitly. To call the constructor, we must explicitly cast the value 10 to a `MyClass` object.

- **Casts:** The `explicit` keyword can also be used with casts to prevent implicit conversions from one type to another. For example, the following code will not compile:

C++

```plaintext
int main() {
  int x = 10;
  float y = static_cast<float>(x); // This will not compile
  return 0;
}
```

In this code, the `static_cast` operator is used to convert the integer `x` to a float `y`. However, because the `static_cast` operator is preceded by the `explicit` keyword, the conversion will not be allowed.

- **Type conversion operators:** The `explicit` keyword can also be used with type conversion operators to prevent implicit conversions from one type to another. For example, the following code will not compile:

C++

```plaintext
class MyClass {
public:
  operator int() const { return 10; }
};

int main() {
  MyClass my_class;
  int x = my_class; // This will not compile
  return 0;
}
```

In this code, the `MyClass` class has a type conversion operator that converts a `MyClass` object to an `int`. However, because the type conversion operator is preceded by the `explicit` keyword, the conversion will not be allowed.


---

Original Source: https://www.mindstick.com/forum/158989/what-does-the-explicit-keyword-mean

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
