Is there any difference between using
this.session["Some_Value"] and session["Some_Value"]
No
"this" in C# points to the current object where the code is located.
No there aint any difference. Session is for the entire user session.
Thanks
Will there be any performace difference
No, I prefer to use this when I call methods and want to access private fields etc..
cant we acess private variable directly?
private variable isnotaccessible outside the class, butaccessible inside the class.
public varialbe is bothaccessible outside and inside the class.
hope it helps
I am not trying to acess the private variable outside the class nor even my doubt is about that.
I would like to know the purpose ofthis keyword while accesible private variables either likethis.PrivateVariableNameor bythis.PublicPropertyName[which gets the private variable]
Check this:
// keywords_this.cs// this exampleusing System;class Employee{ private string name; private stringalias; private decimal salary = 3000.00m; // Constructor: public Employee(stringname, stringalias) { // Use this to qualify the fields, name and alias:this.name =name;this.alias =alias; } // Printing method: public void printEmployee() { Console.WriteLine("Name: {0}\nAlias: {1}", name, alias); // Passing the object to the CalcTax method by using this: Console.WriteLine("Taxes: {0:C}", Tax.CalcTax(this)); } public decimal Salary { get { return salary; } }}class Tax{ public static decimal CalcTax(Employee E) { return 0.08m * E.Salary; }}class MainClass{ static void Main() { // Create objects: Employee E1 = new Employee("John M. Trainer", "jtrainer"); // Display results: E1.printEmployee(); }}
0 comments:
Post a Comment