DEV Community

loading...
Cover image for Data Types and Variables C#

Data Types and Variables C#

saoud profile image Saoud ・3 min read

Terminology


  • REPL stands for Read-Evaluate-Print-Loop. A REPL is a command line tool that allows users to enter lines of code and see the result in the terminal when executed.

Overview


  • You may launch your REPL at any time with the command $ dotnet script.
  • Commands meant to run in the REPL are preceded with a > in our curriculum. Commands meant to run in the standard command line are still preceded with $.
  • Once open, you can exit the REPL with the Ctrl + C command.

Strings and Concatenation Terminology


  • String: A sequence of characters between two quotation marks which is generally used for text content.
  • Concatenate: The act of making a new string out of multiple smaller strings, usually with a + operator, like this: "Hello" + " " + "World", which creates the string "Hello World".

Examples


Here's an example of concatenating multiple strings together in the C# REPL:

> "Programming" + " " + "is" + " " + "awesome!"
"Programming is awesome!"
Enter fullscreen mode Exit fullscreen mode

Tips


  • In C#, all lines of code must end with a semi-colon (;). There are a few exceptions we'll learn later, but don't worry about this for now.
  • We can open the REPL with the $ dotnet script command in the Terminal (for Mac) or Command Prompt (for Windows).

Integers and Arithmetic Terminology


  • Integers: A type of data representing whole numbers (numbers without decimals).
  • Operators: A special character (or characters) that indicates an action to be performed.+/, and % are all operators for mathematic functions.

Examples


Basic arithmetic works just like you'd expect:

1 + 2
4 - 3
5 * 6
9 / 2
7 + 8 * 9
(7 + 8) * 9
Enter fullscreen mode Exit fullscreen mode

Overview


  • 9 % 2 returns the remainder of 9 divided by 2. % is called modulo.

Terminology


  • Assignment Operator: The = operator is used to set the initial value of a variable:
string exampleVariable = "hey, I'm a variable!";`
Enter fullscreen mode Exit fullscreen mode
  • Lower Camel Case: C# uses lower camel case for variables just like JavaScript. Here are a few examples: likeThis, or evenLikeThisExampleHere.
  • Strongly-Typed Language: A language in which data types must be declared. In the following example, we state that our variable has the string data type:
string anotherExampleVariable = "hey, I'm a variable too!";
Enter fullscreen mode Exit fullscreen mode

Overview


  • C# variables must declare the type of data they contain because C# is a strongly typed language.
  • To create a C# variable we need three things:
  • A descriptive name.
  • A data type to declare what data the variable holds such as int or string.
  • An initial value set with the assignment operator (=).

Variables and Data Types Examples


> string phrase = "Hello World";
Enter fullscreen mode Exit fullscreen mode
  • string is the data type our variable will be.
  • phrase is our descriptive name.
  • "Hello World" is the initial value.

Terminology


  • Arguments: Additional information provided to a method, passed in through the parentheses following the method name.
  • Substring: A small portion of a larger string. A bit like a single word in a larger sentence. For instance, "hello" is a substring of the string "hello world".
  • Pascal Case: Required naming convention for all C# methods in which all words are capitalized with no spaces. Examples include LikeThisMethodNameHere() or ThisOtherFictionalMethodHere().

Methods Examples


Here are some examples of built-in methods you can call without arguments in the REPL:

> string phrase = "Programming is AWESOME";

> phrase
"Programming is AWESOME"

> phrase.ToUpper()
"PROGRAMMING IS AWESOME"

> phrase.ToLower()
"programming is awesome"
Enter fullscreen mode Exit fullscreen mode

Calling built-in methods with provided arguments:

> string phraseOne = "hello world";
> string phraseTwo = "hello";

> phraseOne.Contains(phraseTwo)
true
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

pic
Editor guide