FAQ: Data Types and Variables - Creating Variables with Types

This community-built FAQ covers the “Creating Variables with Types” exercise from the lesson "Data Types and Variables ".

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C#

FAQs on the exercise Creating Variables with Types

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

In Data Types and Variables, Lesson 3, Part 1, the code

using System;

namespace Form
{
  class Program
  {
    static void Main(string[] args)
    {
      // Create Variables
      string name = "Shadow", breed = "Golden Retriever";
    


      // Print variables to the console



    }
  }
}

should be accepted, since the convention of naming multiple variables in one line with one declaration is a common one.

Hey I’m new to coding so I know this may be a rookie question. For some reason when I try and set the boolean line I am met with an error what could I be doing wrong on this question?

Hard to say without knowing what you’re doing. Could you post an example?

Why did the compiler insist on double instead of accepting float for the decimal value?
I’m not criticizing, I merely ask to know if there is something I should know, or is it all the same.

Hello, @vladimirkocic! Welcome to the forum!
Most likely, the SCT for the lesson is only accepting double. Doesn’t mean that float wouldn’t work, but the SCT (submitted code tester) isn’t programmed to accept both answers. Keep in mind, that double is more precise than float, so there may have been something in the instructions asking for a specific level of precision.

In Lesson 3, the codeusing System;

namespace Form
{
class Program
{
static void Main(string args)
{
// Create Variables
string name = “Shadow”;
string breed = “Golden Retriever”;
int age = 5;
double weight = 65.22;
bool spayed = true;

  // Print variables to the console
  Console.WriteLine(name);

}

}
}

how to I write all variables (string, int, double) in one Console.WriteLine() ?

Thank!

Hello @text1298343756! Welcome to the forum.
What you are asking will likely be answered in future lessons, but here are examples using the variables from the lessons of three different ways to accomplish the task:

      //concatenation
      Console.WriteLine(name + " " + breed + " " + age + " " + weight + " " + spayed + "\n");
      //composite formatting
      Console.WriteLine("Name: {0}  Breed: {1}\nAge: {2}  Weight: {3}  Spayed: {4}\n", name, breed, age, weight, spayed);
      //string interpolation and a ternary statement
      Console.WriteLine($"My dog's name is {name}. She is a {age} year old {breed}, and weighs {weight} pounds. She {(spayed ? "has": "has not")} been spayed."); 

Output:

Shadow Golden Retriever 5 65.22 True

Name: Shadow Breed: Golden Retriever
Age: 5 Weight: 65.22 Spayed: True

My dog’s name is Shadow. She is a 5 year old Golden Retriever, and weighs 65.22 pounds. She has been spayed.

For more information, search for the terms in my code comments.
Happy coding!

3 Likes

New to C#, coming from python.
Any way I could just iterate each variable like this?

for var in (name, breed, age, weight, spayed){
Console.WriteLine(var)
}

foreach seems to be the closest thing to what I want but it requires a consistent type…

In Data Types and Variables, Lesson 3, Part 1, the code:
using System;

namespace Form
{
class Program
{
static void Main(string args)
{
// Create Variables
string name = “Shadow”;
string breed = “Golden Retriever”;
int age = 5;
double weight = 65.22;
bool spayed = true;

  // Print variables to the console

  Console.WriteLine(name);
  Console.WriteLine(breed);
  Console.WriteLine(age);
  Console.WriteLine(weight);
  Console.WriteLine(spayed);


}

}
}
what is the purpose of “args”?

Not sure what I’m doing wrong here?

Hello @scriptslayer28630.

Welcome to the forums.

I don’t see any problems with your code from the screenshot. It appears that the Next button is active, so your code was accepted by the SCT. You should be able to click Next and proceed. If you’d like to see the output from your code, try refreshing your browser, and click Run again.

Hi. Take care with capitalization, for Console.Writeline is different from Console.WriteLine (where the second L is capitalized.)
Cheers.

1 Like

Sure enough. Need to get my eyes checked :nerd_face: