(7 minutes of reading time) C#, or C-Sharp, is a programming language developed by Microsoft as part of the .NET platform. Its purpose is to facilitate the development process. Come read our article today to understand a little more about C# and its constants and variables

C# – Variables and Constants

(7 minutes of reading time)

As we have seen in previous articles, programming is a field that has gained more prominence with each passing day due to the growing demand present in the job market.

To operate in this market, it is essential to have knowledge of programming languages, a set of rules by which a computer can be instructed to perform a task.

There are many programming languages available for use today, and they are all developed with different purposes in mind. To facilitate your understanding, in today's article, we will talk about variables and constants in C#.

Keep following the content and learn more about the topic!


C#, or C-Sharp, is a programming language developed by Microsoft as part of the .NET platform. Its purpose is to facilitate the development process.

The language was developed to support object orientation and concepts such as encapsulation, inheritance, and polymorphism. Furthermore, it is type-strong and case-sensitive. That is, able to differentiate between lowercase and uppercase letters.

Its typing is static and strong, which means that its variables cannot be typed during the execution of the program.


The language is statically typed, that is, it is composed of a well-defined type of system that is validated throughout the code compile time. For this, you need to define explicit types for the variables that are created.

Static type languages tend to have some advantages, such as a higher level of understanding of how code works and the ability to identify typos more easily. Also, refactoring processes are generally less error prone with static types.

If a parameter of a function needs to change its type, the code will not compile until all places that use that function start specifying the new type.

However, there are also some disadvantages: statically typed languages tend to be more verbose, as you always need to declare the types of variables.

In addition, they can give a false sense of security, especially in cases of newer versions of languages, which tend to work with type inference.


As each type of information has its specific variable, it is essential to know the types of data that a given language offers. Below we list the different C# data types, the values it accepts, and how much space a variable of that type takes up in memory.


Integer numeric types store numbers with no fractional part. Each type accepts a range of values, and the greater this range, the greater the number occupied in memory.

In addition, types whose name comments with the letter “u”, from unsigned, do not accept negative values. They therefore support a wider range of positive values.

Most programmers use the int type to work with integer values. However, you can choose the type that best fits your needs and takes up less memory.


This type is represented by real numbers, that is, those that can have a fractional part.


They are used to store values that can be represented as “true” or “false”, with no intermediate values.


Texts can be stored by char and string types. The char type is used for one character only, while the string is a collection of characters.


To follow a minimum standardization of the source code, C# determines some standards for the definition of names for the variables. The main standards are:

- All variables are named with the first letter in lower case. Except in the case of variables with a composite name, in this scenario, the first letter must be lowercase, while the others must be uppercase;

- Spaces must not be used in the middle of variable names;

- Special characters must not be used to name variables. Except the underscore, in special situations as in the definition of private structures;

- Variables must have declarative and self-explanatory names;

- Variables are case-sensitive.


Constants are like variables, but they cannot be changed. That is, the value cannot be modified during the execution of the application.

The declaration of a constant in C# is done through the keyword “const”.


Type inference is a technique used by compilers to automatically define the types of variables based on context. Which means that the compiler will automatically type the variable at the time of its declaration of the data type being assigned to it.

This process allows the writing of less detailed code, as there is no longer the need to explicitly inform what the type of the variable will be at the time of its declaration. This inference is made by using the reserved word “var”.

It is important to note that type inference does not interfere with C#'s statically typed language status, since the type was defined even if automatically. Furthermore, it should be noted that type inference is only possible if the variable is declared and has its initial value assigned at the same time.


In the case of variables, modifiers define their visibility, whether they can be accessed by classes other than their own, whether they will only be accessed by classes derived from the class they are in, and so on.

In C#, the following modifiers are present:


Access is not restricted.


Access of this modifier is limited to classes or types derived from the class the variable is in.


In this case, access is limited to the current module set.


Access is only allowed to the current set or types derived from the containing class.


Access to this variable is only available for the class it belongs to. Also, when a variable is declared without an access modifier, the compiler assigns the modified default private.

Do you like our content? So, follow us on social media to stay on top of innovation and read our blog.
Share this article on your social networks:
Rate this article:
[yasr_visitor_votes size=”medium”]


Our Latest Articles

Read about the latest trends in technology
Blog 23-05-min
Are you passionate about programming and always looking for ways to excel...
Blog 21-05
Blockchain technology is transforming several industries through decentralized applications (DApps), which operate...

Extra, extra!

Assine nossa newsletter

Fique sempre atualizado com as novidades em tecnologia, transformação digital, mercado de trabalho e oportunidades de carreira

Lorem ipsum dolor sit amet consectetur. Venenatis facilisi.