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 (
) 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 (
) below!
Agree with a comment or answer? 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!
2 Likes
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!
7 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.
2 Likes
Sure enough. Need to get my eyes checked 
Heya,
I’m trying to figure out why my code won’t pass:
namespace DogProgram
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
// Create variables
string name = "Shadow";
string breed = "Golden Retriever";
int age = 5;
double weight = 65.22;
bool spayed = true;
// Print variables to console
Console.WriteLine("The name of the dog is: " + name);
Console.WriteLine("The breed of the dog is: " + breed);
Console.WriteLine("The age of the dog is: " + age);
Console.WriteLine("The weight of the dog is: " + weight);
if(spayed == true)
{
Console.WriteLine("The dog is neutered.");
}
else
{
Console.WriteLine("The dog is not neutered.");
}
}
}
}
It works as intended both on Visual Studio Community 2019 and on the online form.
Hello, @design6292373498, and welcome to the forums.
My best guess, without knowing what the error message from the SCT says, is that the SCT is only expecting the values to be printed. Try removing your extra text, and print only the values. After passing the step, feel free to practice printing the values in any manner you’d like. Happy coding!
1 Like
Next, create a variable to hold the age. Name the variable age
and store the value 5
.
this is what i got. age int = 5; CAN SOMEBODY HELP PLEASE IM STUCK
so I have
using System;
namespace Form
{
class Program
{
static void Main(string[] args)
{
// Create Variables
string name = "Shadow";
string breed = "Golden Retriever";
// Print variables to the console
}
}
}
step 2 does not seem to be activating though, remains shaded out. Can not see what I am doing wrong, even clicked show hint and the format looks exactly as I have it (did orignally forget the ; more used to Kotlin than c# lol)
edit: Never mind, just accidently clicked run and step 2 now shows.
Problem with the lesson. I am skipping this and moving on but am losing confidence in this process.
Submitted this:
using System;
namespace Form
{
class Program
{
static void Main(string args)
{
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);
}
}
}
And got these errors.
Program.cs(13,14): error CS1001: Identifier expected [/home/ccuser/workspace/csharp-data-types-variables-handling-errors-csharp/e7-workspace.csproj]
Program.cs(13,14): error CS1002: ; expected [/home/ccuser/workspace/csharp-data-types-variables-handling-errors-csharp/e7-workspace.csproj]
Program.cs(13,19): error CS1003: Syntax error, ‘(’ expected [/home/ccuser/workspace/csharp-data-types-variables-handling-errors-csharp/e7-workspace.csproj]
Program.cs(13,19): error CS1525: Invalid expression term ‘=’ [/home/ccuser/workspace/csharp-data-types-variables-handling-errors-csharp/e7-workspace.csproj]
Program.cs(13,28): error CS1026: ) expected [/home/ccuser/workspace/csharp-data-types-variables-handling-errors-csharp/e7-workspace.csproj]
The build failed. Fix the build errors and run again.