> For the complete documentation index, see [llms.txt](https://dailyjournal.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dailyjournal.gitbook.io/notes/languages/c-sharp/concepts.md).

# Concepts

## Boxing and Unboxing

* Both Boxing and Unboxing are used for converting the type.

| Boxing                                                                                         | Unboxing                                                                                         |
| ---------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------ |
| The process of converting from *value* type to *reference (object)* type is called **Boxing**. | The process of converting from *reference (object)* type to *value* type in called **Unboxing**. |
| Boxing is an **implicit** conversion.                                                          | Unboxing is an **explicit** conversion.                                                          |

```csharp
int i = 123;
object obj = i;    // boxing

object obj = 123;
int i = (int)obj;    // unboxing
```

##

## Parse and TryParse

| Parse()                                                                                                                                                         | TryParse()                                                                                                                        |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
| returns the converted number                                                                                                                                    | returns a `boolean` value that indicates whether the conversion succeeded, and returns the converted number in an `out` parameter |
| if the string is not in a valid format, `Parse()` throws an exception. Use exception handling to the catch the `FormatException` when the parse operation fails | if the string is not in a valid format, `TryParse()` returns `false`.                                                             |
| `Convert.ToInt32()` uses `Parse()` internally                                                                                                                   |                                                                                                                                   |

## Stateful and Stateless Methods

| Stateful Methods                                                                                                                                                                                | Stateless Methods                                                                                                                                                                         |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Stateful methods are also known as instance methods.                                                                                                                                            | Stateless methods are knowns as static methods.                                                                                                                                           |
| When calling a stateful method, you need to create an instance of the class and access the method on the object.                                                                                | When calling a stateless method, you don't need to create a new instance of its class first.                                                                                              |
| <p>Example: </p><p><code>Random dice = new Random();</code></p><p>In this example, we created the instance of the class(object) so that the method can access the state of the application.</p> | <p>Example: </p><p><code>Console.WriteLine();</code></p><p>In this example, this method performs its function and finishes without impacting the state of the application in any way.</p> |
