DotNet-100-PreWork-GitBook
  • DotNet PreWork
  • Part 0: Intro
    • 1.0: Installation
    • 2.0: Getting Started
    • 3.0: Configuration
  • Part 1: HTML Fundamentals
    • Basic Tags
    • H & P Tags
    • Lists
    • Links
    • Images
    • Tables
    • Divs
    • Inputs
    • Forms
    • Sections
    • Articles & Headers
    • Footers
    • Navs
    • iFrames
  • Part 2: CSS Fundamentals
    • CSS Set Up
    • Classes
    • Ids
    • Margins & Padding
    • Fonts
    • Backgrounds
    • Widths & Heights
    • Borders
    • Positions
  • Part 3: C# Fundamentals
    • 1.0: Hello World
    • 2.0: Variables
    • 3.0: Basic Types
    • 3.0a: Types Challenges
    • 3.0b: Types Challenges Answers
    • 4.0: Operators
    • 5.0: Strings
    • 6.0: Booleans
      • 6.0a: Boolean Challenge
      • 6.0b: Challenge Answers
    • 7.0: Conditionals
      • 7.1: Switch Case
      • 7.2: If Statements
      • 7.3: Ternary Expressions
    • 8.0: Numbers
    • 9.0: Loops
      • 9.0a: Loop Challenges
      • 9.0b: Challenge Answers
    • 10.0: Classes and Objects
    • 11.0: Properties
    • 12.0: Useful-Links
Powered by GitBook
On this page
  • Description
  • File Location
  • Choosing a Startup Project
  • Basic Types Within C
  • Code
  • More Practice
  • Challenges
  • Types Reference For C
  • Visual Representation of 32 bit & 64 bit
  1. Part 3: C# Fundamentals

3.0: Basic Types

Previous2.0: VariablesNext3.0a: Types Challenges

Last updated 6 years ago

In this module we will begin to learn about different data types in C#.

Description

Types will be attached to any part of your application that has to do with your data and values. Every variable, constant, and expression that gives back a value has a type. Every method has a type for the input value. More on what methods and variables are soon.

File Location

  1. Right click on the Solution you made in the previous module. Make sure you're not clicking on the

  2. Go to Add > New Project and choose Console App (.NET Framework).

  3. Name it 03_Types.

  4. You will write your code for this module within Program.cs of that project.

Choosing a Startup Project

As we build these console apps, let us give you an Important Note:. Now that you have more than one project within your solution, you will need to specify which project will run when you click start or when you press CTRL F5. 1. In the Solution Explorer, right click on the project you are working on. 2. Select Set as Startup Project.

Basic Types Within C

Here is a short table of types for C#:

Type

Name

Example

Description

Integer

int

20, -1500

A whole number with a size of 32 bits and range from -2,147,483,648 to 2,147,483,647

Float

float

1.5f

32 bit, non-whole number, up to 7 digits

Double

double

1.500000d

64 bit, up to 15-16 digits

Decimal

decimal

8.333333333333333333m

128 bit, up to 28-29 digits, more precise than double or float, but also more costly. Mainly used for financial situations

Boolean

bool

true, false

A true or false statement

String

string

"Hello World"

A collection of characters with a size of up to 2 gigs

These are some of the basic types C# uses and examples of when to use the different types.

Code

Let's use the table above to add declare and initialize a few variables:

class Program
{
    static void Main(string[] args)
    {
        bool isLoggedIn = true;
        Console.WriteLine(isLoggedIn);

        decimal accountBalance = 80000.30m;
        Console.WriteLine(accountBalance);

        Console.ReadLine();
    }
}

More Practice

Use the above table to try answering the following question:

  • What is the best type for each of these items?

    • 23

    • $3.33

    • 15.25456

    • -3500

    • Hi!

Challenges

Starting in the next module, we will provide a series of challenges for you to work on. Going forward, these will be presented as a series of increasingly difficult challenges. Here are how these challenges work: 1. Bronze: More practice with the current lesson(and some previous ones.) 2. Silver: A challenge that anticipates a concept in the not too distant future. 3. Gold: A harder challenge for a future concept. Feel free to research ahead.

Possible answers will also be provided in a separate module, but try to solve them on your own first. Additionally, there will likely be multiple ways to solve each challenge, so play around and find what works best for you.

Types Reference For C

We have made the following chart for your convenience. We hope this comes in handy for learning C# types.

Type

Description

int i = 0;

A whole number with a size of 32 bits and range from -2,147,483,648 to 2,147,483,647. In the expression, we assign i to the integer 0.

short sh = 32767;

A whole number with size 16 bits and range from -32,768 to 32,767. In the expression we assign sh to 32767.

byte by = 255;

A whole number with size of 8 bits and range from 0 to 255. In the expression we assign by to 255.

string s = "hey!";

A collection of characters with a size of up to 2 gigs or 2^32 bytes, or Int32.MaxValue. In the expression we assign s to hey!

char c = 'i';

A single character with a size of 16 bits. A string is just a linked list of chars. In the expression we assign c to the letter i.

var s2 = @"a \tree";

The @ symbol is used when about to call a reference tree as to specify a directory file on your machine such as \DotNetPreWork\HelloWorld.

bool b = false;

A boolean is a true or false statement. In the expression we assign b to false.

long l = 7;

Long is an integer data type that can just hold more than a normal int type. Exactly 2 times more. The size is 64 bit and the range is -9223372036854775808 to 9223372036854775808. Another way to assign this type is Int64.

decimal p = 99.99999999999999999999999999;

A numeral that can go to a 128-bit precision decimal with a range of 28-29 decimal places. A very costly data type that takes a lot of energy for the program to run. A decimal data type is often used to represent money or other data that needs to be exact.

double d = 7.80000000000000;

A numeral that has a 15-16 digit precision with a 64-bit size.

float f = 10.8f;

Floating point integer, must put f explicit conversion afterwards. Float is similar to a double only it is stored as 32 bits within the memory.

decimal dd = 7.80m;

Without the suffix m, the number is treated as a double and generates a compiler error.

int? ni = null;

When there is a ? afterwards it means the value can be null. Null means it can either have a value there or it can be empty. Null can be used when you are not sure if there will be a value or not such as with web services or database results. Use .HasValue to see if the current Nullable object has a valid value of its underlying type.

Visual Representation of 32 bit & 64 bit

Set as Startup
32&64bitpicture