public void AddPoint(Point2DRef anotherPoint) { this.X = this.X + anotherPoint.X; this.Y = this.Y + anotherPoint.Y; }
public static void Main(string[] args) { //Console.write lines print to console Console.WriteLine("Hello, world!"); //these vars are setting variables to be used later on var meaningOfLife = 42; var smallPi = 3.14f; var bigPi = 3.14159265359; var vaporWare = "Half Life 3"; const bool likesPizza = true; string[] writers = { "Anthony", "Brian", "Eric", "Sean" }; string[] editors = new string[5]; Console.WriteLine(writers[2]); //these are checking to see if certain conditions are met before they execute the code within writers[0] = "Ray"; //I got rid of useless code due to this being a boolean. They had it as a == flase when it works just as well to put an ! at the beginning. if (!likesPizza) { Console.WriteLine("You monster!"); } //this was actually interesting to look into. turns out that this is a single line if else statement. This is pretty neat to know for future refrence. bool isMonster = (likesPizza == true) ? false : true; for (int i = 0; i < 10; i++) { Console.WriteLine("C# Rocks!"); } for (int i = 0; i < writers.Length; i++) { string writer = writers[i]; Console.WriteLine(writer); } foreach (string writer in writers) { Console.WriteLine(writer); } if (meaningOfLife == 42) { bool inScope = true; } //creating a 2d object/shape and then attaching things to it. Point2D myPoint = new Point2D(); myPoint.X = 10; myPoint.Y = 20; Point2D anotherPoint = new Point2D(); anotherPoint.X = 5; anotherPoint.Y = 15; myPoint.AddPoint(anotherPoint); Console.WriteLine(myPoint.X); Console.WriteLine(myPoint.Y); Point2D yetAnotherPoint = myPoint; yetAnotherPoint.X = 100; Console.WriteLine(myPoint.X); Console.WriteLine(yetAnotherPoint.X); Point2DRef pointRef = new Point2DRef(); pointRef.X = 20; Point2DRef anotherRef = pointRef; anotherRef.X = 25; Console.WriteLine(pointRef.X); Console.WriteLine(anotherRef.X); pointRef = null; anotherRef.X = 125; Console.WriteLine(anotherRef.X); anotherRef = null; RenFairePerson person = new RenFairePerson(); person.Name = "Igor the Ratcatcher"; person.SayHello(); }
public static void Main(string[] args) { //Your code goes here Console.WriteLine("Hello, world!"); var meaningOfLife = 42; var smallPi = 3.14f; var bigPi = 3.14159265359; var vaporWare = "Half Life 3"; const bool likesPizza = true; string[] writers = { "Anthony", "Brian", "Eric", "Sean" }; string[] editors = new string[5]; Console.WriteLine(writers[2]); writers[0] = "Ray"; if (likesPizza == false) { Console.WriteLine("You monster!"); } bool isMonster = (likesPizza == true) ? false : true; for (int i = 0; i < 10; i++) { Console.WriteLine("C# Rocks!"); } for (int i = 0; i < writers.Length; i++) { string writer = writers[i]; Console.WriteLine(writer); } foreach (string writer in writers) { Console.WriteLine(writer); } if (meaningOfLife == 42) { bool inScope = true; } Point2D myPoint = new Point2D(); myPoint.X = 10; myPoint.Y = 20; Point2D anotherPoint = new Point2D(); anotherPoint.X = 5; anotherPoint.Y = 15; myPoint.AddPoint(anotherPoint); Console.WriteLine(myPoint.X); Console.WriteLine(myPoint.Y); Point2D yetAnotherPoint = myPoint; yetAnotherPoint.X = 100; Console.WriteLine(myPoint.X); Console.WriteLine(yetAnotherPoint.X); Point2DRef pointRef = new Point2DRef(); pointRef.X = 20; Point2DRef anotherRef = pointRef; anotherRef.X = 25; Console.WriteLine(pointRef.X); Console.WriteLine(anotherRef.X); pointRef = null; anotherRef.X = 125; Console.WriteLine(anotherRef.X); anotherRef = null; RenFairePerson person = new RenFairePerson(); person.Name = "Igor the Ratcatcher"; person.SayHello(); }
public static void Main(string[] args) { //Writing to the console Console.WriteLine("Hello, world!"); //making variables var meaningOfLife = 42; var smallPi = 3.14f; var bigPi = 3.14159265359; var vaporWare = "Half Life 3"; const bool likesPizza = true; //making arrays string[] writers = { "Anthony", "Brian", "Eric", "Sean" }; string[] editors = new string[5]; Console.WriteLine(writers[2]); writers[0] = "Ray"; //if statements if (likesPizza == false) { Console.WriteLine("You monster!"); } //conditional, if likesPizza is true, isMonster is false, else it is true bool isMonster = (likesPizza == true) ? false : true; //for loops for (int i = 0; i < 10; i++) { Console.WriteLine("C# Rocks!"); } for (int i = 0; i < writers.Length; i++) { string writer = writers[i]; Console.WriteLine(writer); } //foreach loop goes through each writer in the array foreach (string writer in writers) { Console.WriteLine(writer); } if (meaningOfLife == 42) { bool inScope = true; } //structs, uses the methods/functions below //gives myPoint two coordinates Point2D myPoint = new Point2D(); myPoint.X = 10; myPoint.Y = 20; Point2D anotherPoint = new Point2D(); anotherPoint.X = 5; anotherPoint.Y = 15; //adds another point's x and y to myPoint myPoint.AddPoint(anotherPoint); Console.WriteLine(myPoint.X); Console.WriteLine(myPoint.Y); Point2D yetAnotherPoint = myPoint; yetAnotherPoint.X = 100; Console.WriteLine(myPoint.X); Console.WriteLine(yetAnotherPoint.X); //reference point, exists in long term memory rather than short term Point2DRef pointRef = new Point2DRef(); pointRef.X = 20; Point2DRef anotherRef = pointRef; anotherRef.X = 25; Console.WriteLine(pointRef.X); Console.WriteLine(anotherRef.X); //removing references, garbage cleanup pointRef = null; anotherRef.X = 125; Console.WriteLine(anotherRef.X); anotherRef = null; //stuct for a person, who inherits the attributes the Person class, including the SayHello function RenFairePerson person = new RenFairePerson(); person.Name = "Igor the Ratcatcher"; person.SayHello(); }
public static void Main(string[] args) { //Your code goes here Console.WriteLine("Hello, world!"); //C# can infer variable types. var meaningOfLife = 42; var smallPi = 3.14; var bigPi = 3.14159265359; var vaporWare = "Half Life 3"; //const var likesPizza = true; - This will throw an error: You cannot make a variable with an inferred type constant. const bool likesPizza = true; //This creates an array of strings. string[] writers = { "Anthony", "Brian", "Eric", "Sean" }; //This creates an array of strings with a capacity of 5 elements. string[] editors = new string[5]; //This prints the third element in the array to the console. Console.WriteLine(writers[2]); //This replaces the first element in the array. writers[0] = "Ray"; //If likesPizza is false, prints the message to the console. if (likesPizza == false) { Console.WriteLine("You monster!"); } //This is another way to write an "If" statement. bool isMonster = (likesPizza == true) ? false : true; //The "for" loop initializes the variable i and sets it to 0; it then counts up by 1 each time the loop runs until i = 10. //It will write the message 10 times in the console. for (int i = 0; i < 10; i++) { Console.WriteLine("C# Rocks!"); } //This "for" loop will write all of the elements in the writers array to the console, using the capacity of the array to determine how many times to loop. for (int i = 0; i < writers.Length; i++) { string writer = writers[i]; Console.WriteLine(writer); } //"foreach" loops are useful for arrays. foreach (string writer in writers) { Console.WriteLine(writer); } if (meaningOfLife == 42) { bool inScope = true; } //inScope = false; - This will throw an error, because the variable "inScope" was defined within the scope of the "if" statement, and therefore does not exist outside of it. //This creates an instance of the struct. Point2D myPoint = new Point2D(); //You can set the values of the instance by referencing its instance name. myPoint.X = 10; myPoint.Y = 20; Point2D anotherPoint = new Point2D(); anotherPoint.X = 5; anotherPoint.Y = 15; //This will add the two points together. myPoint.AddPoint(anotherPoint); Console.WriteLine(myPoint.X); Console.WriteLine(myPoint.Y); //This creates another instance of Point2D based on myPoint. Point2D yetAnotherPoint = myPoint; //Then we can change any of the values while keeping the others the same. yetAnotherPoint.X = 100; Console.WriteLine(myPoint.X); Console.WriteLine(yetAnotherPoint.X); //This creates an instance of Point2DRef named pointRef and sets its X value to 20. Point2DRef pointRef = new Point2DRef(); pointRef.X = 20; //This creates an instance of Point2DRef named anotherRef and sets its X value to 25. Point2DRef anotherRef = pointRef; anotherRef.X = 25; //When you assign null to a variable, the object is no longer accessible. //However, because there is another reference to pointRef, it is still in memory and thus accessible. pointRef = null; anotherRef.X = 125; Console.WriteLine(anotherRef.X); //Now that there are no more references to the object, garbage collection can occur, which removes objects from memory that do not have any references. anotherRef = null; RenFairePerson person = new RenFairePerson(); person.Name = "Igor the Ratcatcher"; person.SayHello(); }
public static void Main(string[] args) { //Your code goes here Console.WriteLine("Hello, world!"); var meaningOfLife = 42; var smallPi = 3.14f; var bigPi = 3.14159265359; const bool likesPizza = true; string[] writers = { "Anthony", "Brian", "Eric", "Sean" }; string[] editors = new string[5]; //this creates an array with a capacity of 5 elements - "New" is creating an instance of the array of strings //important when working with objects Console.WriteLine(writers[2]); //you will see eric's name printed in the code - this is because array's always start with zero and count on //from there - so Anthony is 0, Brian is 1, and Eric is 2. writers[0] = "Ray"; //this replaces the first string in the array with another string - now when the first element is accessed, it will read //ray instead of anthony. //cannot remove indexes - once an array has been defined with an amout of elements, it will always contain that number of elements. If you want to //copy the array to another array, you have to create a new array and copy all of the values. //control the code flows based on certain values - i.e. an if statement if (likesPizza == false) { Console.WriteLine("You Monster!"); bool isMonster = (likesPizza == true) ? false : true; //This is a condenced if statement of the one written above } //For Loop - this loop works to initialze a variable named i, then loops 10 times, all the while incrementing i after each iteration for (int i = 0; i < 10; i++) { Console.WriteLine("C# Rocks"); } foreach (string writer in writers) //this loops through all the elements of the array and prints each element to the console. { Console.WriteLine(writer); } if (meaningOfLife == 42) { bool inScope = true; } Point2D myPoint = new Point2D(); myPoint.X = 10; myPoint.Y = 20; //the dot operater here allows us to access the properties. Point2D anotherPoint = new Point2D(); anotherPoint.X = 5; anotherPoint.Y = 15; myPoint.AddPoint(anotherPoint); Console.WriteLine(myPoint.X); Console.WriteLine(myPoint.Y); Point2D yetAnotherPoint = myPoint; yetAnotherPoint.X = 100; Console.WriteLine(myPoint.X); Console.WriteLine(yetAnotherPoint.X); Point2DRef pointRef = new Point2DRef(); pointRef.X = 20; Point2DRef anotherRef = pointRef; anotherRef.X = 25; Console.WriteLine(pointRef.X); Console.WriteLine(anotherRef.X); pointRef = null; //makes the object no longer accesible, unless there is another reference to it. - here anotherRef is refering to pointRef so its not null anotherRef.X = 125; Console.WriteLine(anotherRef.X); anotherRef = null; RenFairePerson person = new RenFairePerson(); person.Name = "Igor the Ratcatcher"; person.SayHello(); }
public static void Main(string[] args) { //Your code goes here Console.WriteLine("Hello, world!"); //Make the console say Hello, world! //Make sure to type the comma because dats good grammar. int meaningOfLife = 42; //assign an integer variable. float smallPi = 3.14f; // assign a float and use f to specify that it's a float. double bigPi = 3.14159265359; //double because it's double the size. not really, it's just bigger. string vaporWare = "Half Life 3"; //string because letters. bool likesPizza = true; //true or false string[] writers = { "Anthony", "Brian", "Eric", "Sean" }; //This is an array of strings. string[] editors = new string[5]; // You can declare an array without assigning the values bool isMonster = (likesPizza == true) ? false : true; // this is a quicker way to assign a value. It's like an if statement. writers[0] = "Ray"; //This reassigns the first element of the array. Console.WriteLine(writers[2]); // this writes the second element of the array to the console. (Brian) if (likesPizza == false) { Console.WriteLine("You monster!"); // if likesPizza is false, dehumanize the user. } // set up a for loop. declare a variable, // set up the boolean statement, and increment the variable. //This loops 10 times. for (int i = 0; i < 10; i++) { Console.WriteLine("C# Rocks!"); //Writes the message 10 times. } //This loops through each element of the array and writes them. for (int i = 0; i < writers.Length; i++) { string writer = writers[i]; Console.WriteLine(writer); } //Here's another kind of loop that is great for arrays. foreach (string writer in writers) { Console.WriteLine(writer); } if (meaningOfLife == 42) { bool inScope = true; } //inScope = false; //This will cause an error because it is out of scope. Point2D myPoint = new Point2D(); //declares new structure object myPoint.X = 10; myPoint.Y = 20; Point2D anotherPoint = new Point2D(); //declares a second structure object anotherPoint.X = 5; anotherPoint.Y = 15; myPoint.AddPoint(anotherPoint); //passes the second structure object to the AddPoint function of the first object Point2D yetAnotherPoint = myPoint; //assigns a new object to the same location as myPoint yetAnotherPoint.X = 100; //assigns the x to 100; Console.WriteLine(myPoint.X); Console.WriteLine(yetAnotherPoint.X); Point2DRef pointRef = new Point2DRef(); pointRef.X = 20; Point2DRef anotherRef = pointRef; anotherRef.X = 25; //Both pointRef and anotherRef x will be assigned to 25 because of the way class memory works. pointRef = null; //even though this is set to null, the class object still has a reference with anotherRef anotherRef.X = 125; // You can still set this. Console.WriteLine(anotherRef.X); // write it anotherRef = null; // now the class is null RenFairePerson person = new RenFairePerson(); //Adds a class object person.Name = "Igor the Ratcatcher"; //assigns a name in the class (to an existing string variable) person.SayHello(); //calls the sayHello() function but it's overrided to say Huzzah! }
public static void Main(string[] args) { //Your code goes here Console.WriteLine("Hello, world!"); //writing lines in C# var meaningOfLife = 42; //examples of varibale types and instatiation var smallPi = 3.14f; //Best practice is to declare int, float, bool, etc but types can also be inferred by the processor var bigPi = 3.14159265359; //A double uses twice as much data to store as a float var vaporWare = "Half Life 3"; const bool likesPizza = true; //can't change constants string[] writers = { "Anthony", "Brian", "Eric", "Sean" }; //Instantiates an array string[] editors = new string[5]; //Instantiates and empty array with 5 open elements Console.WriteLine(writers[2]); //writes the second element of the array writers[0] = "Ray"; //replaces first element in the array with new string if (likesPizza == false) { Console.WriteLine("You monster!"); } bool isMonster = (likesPizza == true) ? false : true; //short hand of above if statement for (int i = 0; i < 10; i++) //loops 10 times incrementing by 1 each time { Console.WriteLine("C# Rocks!"); } for (int i = 0; i < writers.Length; i++) //loops through the writers array and prints each element { string writer = writers[i]; Console.WriteLine(writer); } foreach (string writer in writers) //does the same as above but in foreach format { Console.WriteLine(writer); } if (meaningOfLife == 42) //example of variable scope. inScope is only in scope inside the braces { bool inScope = true; } Point2D myPoint = new Point2D(); //creates new point from the struct below myPoint.X = 10; //changes the x and y properties of this new point myPoint.Y = 20; Point2D anotherPoint = new Point2D(); //creates another point to add to myPoint in the below struct anotherPoint.X = 5; anotherPoint.Y = 15; myPoint.AddPoint(anotherPoint); //Calling the method inside of the struct to add the points Console.WriteLine(myPoint.X); //results of method Console.WriteLine(myPoint.Y); Point2D yetAnotherPoint = myPoint; //copied another instance of the struct yetAnotherPoint.X = 100; Console.WriteLine(myPoint.X); Console.WriteLine(yetAnotherPoint.X); Point2DRef pointRef = new Point2DRef(); //creates instance of point2DRef object pointRef.X = 20; //assigns x value Point2DRef anotherRef = pointRef; //copied to new instance anotherRef.X = 25; //x value is changed again //These are two references to the same object and therefore both now have 25 as the x value Console.WriteLine(pointRef.X); Console.WriteLine(anotherRef.X); pointRef = null; //object is still in memory and therefore still accessible even though it is set to null anotherRef.X = 125; Console.WriteLine(anotherRef.X); //outputs 125 anotherRef = null; RenFairePerson person = new RenFairePerson(); //Creates new object of class person person.Name = "Igor the Ratcatcher"; //sets the name person.SayHello(); }
public static void Main(string[] args) { //Your code goes here Console.WriteLine("Hello, world!"); int meaningOfLife = 42; float smallPi = 3.14f; double bigPi = 3.14159265359; string vaporWare = "Half Life 3"; const bool likesPizza = false; //const makes the variable immutable string[] writers = { "Anthony", "Brian", "Eric", "Sean" }; string[] editors = new string[5]; //we don't know the editors yet Console.WriteLine(writers[2]); if (likesPizza == false) { Console.WriteLine("You monster!"); //barf noises } bool isMonster = (likesPizza == true) ? false : true; for (int i = 0; i < 10; i++) { Console.WriteLine("C# Rocks!"); } foreach (string writer in writers) { Console.WriteLine(writer); } if (meaningOfLife == 42) { bool inScope = true; //inScope only exists within the scope of this if statement } Point2D myPoint = new Point2D(); myPoint.X = 10; myPoint.Y = 20; Point2D anotherPoint = new Point2D(); anotherPoint.X = 5; anotherPoint.Y = 15; myPoint.AddPoint(anotherPoint); Console.WriteLine(myPoint.X); Console.WriteLine(myPoint.Y); Point2D yetAnotherPoint = myPoint; yetAnotherPoint.X = 100; //replicated myPoint, but changed the x value Console.WriteLine(myPoint.X); Console.WriteLine(yetAnotherPoint.X); Point2DRef pointRef = new Point2DRef(); pointRef.X = 20; Point2DRef anotherRef = pointRef; anotherRef.X = 25; Console.WriteLine(pointRef.X); Console.WriteLine(anotherRef.X); //now the points return as the same pointRef = null; anotherRef.X = 125; Console.WriteLine(anotherRef.X); //outputs 125 RenFairePerson person = new RenFairePerson(); person.Name = "Igor the Ratcatcher"; person.SayHello(); }