static void Main(string[] args) { string j = ""; for (int i = 0; i < 10; i++) { j = i.ToString(); k = i.ToString(); Console.WriteLine(i); if (i == 9) { string l = i.ToString(); } //Console.WriteLine(l); } //Console.WriteLine(i); Console.WriteLine("Outside of the for: " + j); Console.WriteLine("Outside of the for: " + k); HelperMethod(); Car myCar = new Car(); myCar.DoSomething(); Console.ReadLine(); }
static void Main(string[] args) { string j = ""; for (int i = 0; i < 10; i++) { j = i.ToString(); k = i.ToString(); Console.WriteLine(i); } //invalid. outside of for loop scope. Console.WriteLine(i); // j is accesible inside and outside for loop. Console.WriteLine(j); // k is accesible as well. Console.WriteLine(k); // k is accessible because it is defined at the "class-level" HelperMethod(); // instatiate car class Car myCar = new Car(); // calls the public method DoSomething(). // can't call the Car class's helperMethod directly, because it's private myCar.DoSomething() // -> hello world! Console.ReadLine(); }
static void Main(string[] args) { Car myCar = new Car(); myCar.DoSomething(); Console.ReadLine(); }
//private static string k = ""; static void Main(string[] args) { /* string j = ""; for (int i = 0; i < 10; i++) { j = i.ToString(); k = i.ToString(); Console.WriteLine(i); if (i == 9) { string l = i.ToString(); } //Console.WriteLine(l); < --error } //Console.WriteLine(i); <-- error Console.WriteLine("Outside of the for: {0}", j); Console.WriteLine("k: {0}", k); HelperMethod(); */ Car car = new Car(); car.DoSomething(); Console.ReadLine(); }
static void Main(string[] args) { string j = ""; for (int i = 0; i < 10; i++) { j = i.ToString(); k = i.ToString(); Console.WriteLine(i); if (i == 9) { string l = i.ToString(); } //Console.WriteLine("l: " l); here l wont be accessible as well, only inside the if } //The variable i is not accessible from here, outside the for //Console.WriteLine(i); Console.WriteLine("Outside of the for: " + j); //Console.WriteLine("k: " + k); K will be accessible because it was declared inside the main helperMethod();//calling the method with the value of K Car car = new Car(); car.DoSomething(); Console.ReadLine(); }
static void Main(string[] args) { string j = ""; Console.WriteLine("Start printing inside of the for-loop"); for (int i = 0; i < 10; i++) { Console.WriteLine(i); j = i.ToString(); k = i.ToString(); if (i == 9) { string l = i.ToString(); } } Console.WriteLine("Printing outside of the for-loop: {0}", j); Console.WriteLine("Printing outside of the for-loop (method prop): {0}", k); HelperMethod(); Car car = new Car(); car.DoSomething(); Console.ReadLine(); }
private static string k = ""; // private field - sort of like a property that is available to all of the memebers of the class static void Main(string[] args) //main method of class Program { string j = ""; //local property (value) since it is inside of the Main method - it is only available inside of Main() for (int i = 0; i < 10; i++) { j = i.ToString(); k = i.ToString(); Console.WriteLine(i); if (i == 9) { string l = i.ToString(); Console.WriteLine(""); Console.WriteLine("i = " + l); Console.WriteLine(""); } //Console.WriteLine("i= " + l); // again - get error because l is defined inside of the if (i == 9) statement etc etc } // Console.WriteLine(i); // causes error because i at this time is only operting within the for loop abve and is not defined in this context (part of the code) // display the i variable via the j property that we defined INSIDE of the method, but outside of the context (For loop) Console.WriteLine("Outside of the For loop - " + j); // now display the value of i via the k variable, which we establish as a static field (property) available to all of the class Console.WriteLine("Outside of the For loop - " + k); // call helper method called HelperMethod to return the k value - this works since it was established at the class level, which makes it avaialbel to all of the code in the class HalperMethod(); Car myCar = new Car(); myCar.DoSomething(); //I can only see this method fron the Car class because it is PUBLIC Console.ReadLine(); }
public static void Main(string[] args) { string j = ""; for (int i = 0; i < 10; i++) { j = i.ToString(); k = i.ToString(); Console.WriteLine(i); // In this case the l is not accessible because it's inside of an if/else statement // if (i == 9) // { // string l = i.ToString(); // } // Console.WriteLine(l); } Console.WriteLine("Outside of the for: " + j); Console.WriteLine("Outside of the for: " + k); Car myCar = new Car(); myCar.DoSomething(); HelperMethod(); Console.ReadLine(); }
// private static string k = ""; static void Main(string[] args) { /* * string j = ""; * * for (int i = 0; i < 10; i++) * { * j = i.ToString(); * k = i.ToString(); * Console.WriteLine(i); * * if (i == 9) * { * string l = i.ToString(); * } * //Console.WriteLine("l: " + l); * } * //Console.WriteLine(i); * * Console.WriteLine("Outsite of the for: " + j); * //Console.WriteLine("k: " + k); * helperMethod(); */ Car car = new Car(); car.DoSomething(); Console.ReadLine(); }
static void Main(string[] args) { string j = ""; for (int i = 0; i < 10; i++) { j = i.ToString(); k = i.ToString(); Console.WriteLine(i); if (i == 9) { string l = i.ToString(); } //Console.WriteLine(l); //printing out here would be outside the scope of L. } Console.WriteLine("Outside of the for loop: j = " + j); Console.WriteLine("Outside of the for loop: k = " + k); HelperMethod(); Car myCar = new Car(); myCar.DoSomething(); Console.ReadLine(); }
static void Main(string[] args) { string j = ""; for (int i = 0; i < 10; i++) { j = i.ToString(); k = i.ToString(); Console.WriteLine(i); if (i == 9) { string l = i.ToString(); } // doesnt work as l is called in a different codeblock not related to this one //Console.WriteLine(l); Console.WriteLine("Outside of the for: " + j); Console.WriteLine("Outside of the for: " + k); HelperMethod(); Car myCar = new Car(); myCar.DoSomething(); } void HelperMethod() { Console.WriteLine("Value of k from the HelperMethod():" + k); } }
static void Main(string[] args) { string j = ""; for (int i = 0; i < 10; i++) { j = i.ToString(); k = i.ToString(); Console.WriteLine(i); if (i == 9) { string l = i.ToString(); } //Console.WriteLine(l); // out of scope because l is declared within the if statement. } Console.WriteLine("Outside of the for: " + j); //works because j was declared outside the for block Console.WriteLine("Outside of the for: " + k); HelperMethod(); Car myCar = new Car(); myCar.DoSomething(); Console.ReadLine(); }
static void Main(string[] args) { string j = ""; for (int i = 0; i < 10; i++) { j = i.ToString(); k = i.ToString(); Console.WriteLine(i); if (i == 9) { string l = i.ToString(); } // Console.WriteLine(l); } // Console.WriteLine(i); Console.WriteLine("Outside the for: " + j); Console.WriteLine("Outside the for: " + k); HelperMethod(); //////////////////////////////////////////////////////// Car myCar = new Car(); // you should not see helperMethod() in the intellisense popup myCar.DoSomething(); //////////////////////////////////////////////////////// Console.ReadLine(); }
public static void Main(string[] args) { string j = ""; for (int i = 0; i < 10; i++) { j = i.ToString(); k = i.ToString(); Console.WriteLine(i); if (i == 9) { string l = i.ToString(); } //Console.WriteLine(l); } //Console.WriteLine(i); Console.WriteLine("Outside of the for: " + j); Console.WriteLine("Outside of the for: " + k); HelperMethod(); Car myCar = new Car(); myCar.DoSomething(); Console.ReadLine(); }
static void Main(string[] args) { string j = ""; for (int i = 0; i < 10; i++) { j = i.ToString(); k = i.ToString(); Console.WriteLine(i); if (i == 9) { string l = i.ToString(); } //Console.WriteLine(l); //error, l is out of scope } //Console.WriteLine(i); //error, i is out of scope Console.WriteLine(j); //outside the for: 9 Console.WriteLine(k); //also 9 HelperMethod(); Car myCar = new Car(); myCar.DoSomething(); // helperMethod not accessible because private Console.ReadLine(); }
//private static string k = ""; static void Main(string[] args) { /* string j = ""; for (int i = 0; i < 10; i++) { j = i.ToString(); k = i.ToString(); Console.WriteLine(i); if (i == 9) { string l = i.ToString(); // Console.WriteLine("L: " + l); } } Console.WriteLine("Outside of the for: " + j); //Console.WriteLine("K: " + k); helperMethod();*/ Car car = new Car(); car.DoSomething(); Console.ReadLine(); }
static void Main(string[] args) { string j = ""; for (int i = 0; i < 10; i++) { j = i.ToString(); k = i.ToString(); Console.WriteLine(i); if (i == 9) { string l = i.ToString(); } } Console.WriteLine("Outside the for looop for j:" + j); Console.WriteLine("Outside the for looop for inner k:" + k); HelperMethod(); Car myCar = new Car(); myCar.DoSomething(); //Can only call the public method from here not the private string helperMethod Console.ReadLine(); }
// For example 1: // private static string k = ""; static void Main(string[] args) { // Example 2 - Accessibility Modifiers Car car = new Car(); car.DoSomething(); Console.ReadLine(); }
// Declaring at this level makes it a field of the class //private static string k = ""; static void Main(string[] args) { //string j = ""; //for (int i = 0; i < 10; i++) //{ // j = i.ToString(); // k = i.ToString(); // Console.WriteLine(i); // if (i == 9) // { // string l = i.ToString(); // } // // This will not compile, as i is scoped to the condition block // //Console.WriteLine("l: " + l); //} //// This will not compile, as i is scoped to the for loop only ////Console.WriteLine(i); //// This will compile, as j is declared outside of the foor loop, and accessible from both inside and outside of the loop //Console.WriteLine("Outside of the for: " + j); //// This will compile, as k is declared at the class level, and accessible anywhere within the class' children ////Console.WriteLine("k: " + k); //// This helperMethod call is a child of the class level, and therefore will know about k within the method //HelperMethod(); Car car = new Car(); // GOOD car.DoSomething(); // WILL NOT WORK //car.HelperMethod(); Console.ReadLine(); }
private static string k = ""; // accessible by anything within class static void Main(string[] args) { string j = ""; for (int i = 0; i < 10; i++) // can only access "i" within for loop { string l = ""; // string l is only accessible within for loop j = i.ToString(); k = i.ToString(); // here you can still access k and insert values Console.WriteLine(i); } Console.WriteLine("Outside of the for loop: " + j); // j = 9 Console.WriteLine("Outside of the for loop: " + k); HelperMethod(); // can call this method without instantiation b/c static // instantiate car object Car myCar = new Car(); myCar.DoSomething(); // calling on a public method Console.ReadLine(); }
// private vs public // both are accessibility modifiers, used to implement OOP concept called encapsulation // think of public methods as interfaces, where you can see and use // think of private methods as behind-the-scene, you can't see // A private method can be called/used by any other members/methods inside the class, but not for outsiders static void Main(string[] args) { string j = ""; for (int i = 0; i < 10; i++) { j = i.ToString(); k = i.ToString(); Console.WriteLine(i); } Console.WriteLine("J outside of the for loop: {0}", j); Console.WriteLine("K outside of the for loop: {0}", k); HelperMethod(); Car myCar = new Car(); myCar.DoSomething(); // note: you can use DoSomething(), but not helperMethod() // encapsulation: you want to hide the implementation of our classes Console.ReadLine(); }
//private static string k = ""; static void Main(string[] args) { // string j = ""; // for (int i = 0; i < 10; i++) // { // j = i.ToString(); // k = i.ToString(); // Console.WriteLine(i); // if (i == 9) // { // string l = i.ToString(); // } // } // Console.WriteLine("Outside of the for: " + j); // //Console.WriteLine("k: " + k); // helperMethod(); Car car = new Car(); car.DoSomething(); Console.ReadLine(); }
static void Main(string[] args) { // Testing how variable scope works string j = ""; for (int i = 0; i < 10; i++) { j = i.ToString(); k = i.ToString(); Console.WriteLine(i); if (i == 9) { string l = i.ToString(); } // l is outside of if scope //Console.WriteLine(l); } // i is outside of for scope //Console.WriteLine(i); Console.WriteLine("outside of the for: " + j); Console.WriteLine("outside of the for: " + k); HelperMethod(); Car myCar = new Car(); // only the public methods are visible to us myCar.DoSomething(); Console.ReadLine(); }
// private and public are called accessibility modifiers to implements // encapsulation. // encapsulation: all the important behind the scenes functionality should be // hidden in interfaces and classes. // but consumer of the class shouldn't have to know any of the inner workings // of the class to be able to use it. public static void Main(string[] args) { string j = ""; for (int i = 0; i < 10; i++) { // simple for loop printing 0 - 9. j = i.ToString(); k = i.ToString(); Console.WriteLine(i); if (i == 9) { string l = i.ToString(); } // Console.WriteLine(l); // ^ doesn't work since scope is only the if statement. } //Console.WriteLine(i); // ^ does not work because it is outside of the scope of its definition. Console.WriteLine("Outside of the for: {0}", j); // declared outside for loop so it can be used. Console.WriteLine("Outside of the for: {0}", k); // declared private in this class so it can be used. HelperMethod(); Car myCar = new Car(); myCar.DoSomething(); // you know that this will print out hello world, but you don't know // how it does this. }
static void Main(string[] args) { Car myCar = new Car(); myCar.DoSomething(); Console.ReadLine(); }