Posted by: Amit Andharia | January 3, 2009

Public Access Modifier

 

The public keyword is an access modifier for types and type members. Public access is the most permissive access level. There are no restrictions on accessing public members, as in this example:

class SampleClass
{

public int x; // No access restrictions.
}
// Public access
using System;
class Location
{
public int x;
public int y;
}

Output:
x = 10, y = 15If you change the public access level to private or protected, you will get the error message:

‘Location.y’ is inaccessible due to its protection level.

class MainClass
{

Locationl = new Location();
// Direct access to public members:
l.x = 10;
l.y = 15;
Console.WriteLine(“x = {0}, y = {1}”, l.x, l.y);

static void Main(){}

}

Example:
In the following example, two classes are declared, Location and MainClass. The public members x and y of Location are accessed directly from MainClass.

Posted by: Amit Andharia | January 3, 2009

Protected Access Modifier

The protected keyword is a member access modifier. A protected member is accessible within its class and by derived classes.

A protected member of a base class is accessible in a derived class only if the access takes place through the derived class type. For example, consider the following code segment:

using System;
class B

protected int x = 123; 

}

class D : B
{

 static void Main()
 {

  B b = new B();
  D d = new D();

  // Error CS1540, because x can only be accessed by
  // classes derived from B.
  // b.x = 10;

  // OK, because this class derives from B.
  d.x = 10;

 }

}

The statement b.x =10 generates an error because B is not derived from D.

Struct members cannot be protected because the struct cannot be inherited.

Example:
In this example, the class DerivedLocation is derived from Location; therefore, you can access the protected members of the base class directly from the derived class.

using System;
class Location
{

protected int x;
protected int y;

}
class DerivedLocation: Location
{

static void Main()
{

DerivedLocation dlocation = new DerivedLocation();

// Direct access to protected members:
dlocation.x = 10;
dlocation.y = 15;
Console.WriteLine(“x = {0}, y = {1}”, dlocation.x, dlocation.y);

}

}

Output:
x = 10, y = 15

Posted by: Amit Andharia | January 3, 2009

Private Access Modifier

 

The private keyword is a member access modifier. Private access is the least permissive access level. Private members are accessible only within the body of the class or the struct in which they are declared, as in this example:

class Employee
{

private int k;
double l; // private access by default

}

It is a compile-time error to reference a private member outside the class or the struct in which it is declared.

Posted by: Amit Andharia | January 2, 2009

C# Keywords & Terminology

 

  • Access modifier: A keyword, such as private, protected, internal, or public, that restricts access to a type or type member.
  • Accessible member: A member that can be accessed by a given type. An accessible member for one type is not necessarily accessible to another type.
  • Accessor: A method that sets or retrieves the value of a private data member value that is associated with a property. Read-write properties have get and set accessors. Properties that are read-only have only a get accessor.
  • Anonymous method: An anonymous method is a code block that is passed as a parameter to a delegate.
  • Base class: A class that is inherited by another ‘derived’ class.
  • Call stack: The series of method calls leading from the beginning of the program to the statement currently being executed at run time.
  • Class: A data type that describes an object. Classes contain both data, and the methods for acting on the data.
  • Constructor: A special method on a class or struct that initializes the objects of that type.
  • Delegate: A delegate is a type that references a method. Once a delegate is assigned a method, it behaves exactly like that method.
  • Derived class: A class that uses inheritance to gain, augment, or modify the behavior and data of another ‘base’ class.
  • Destructor: A special method on a class or struct that prepares the instance for destruction by the system.
  • Event: A member of a class or struct that sends notifications of a change.
  • Field: A data member of a class or struct that is accessed directly.
  • Generics: Generics allow you to define a class and or method that are defined with a type parameter. When client code instantiates the
    type, it specifies a particular type as an argument.
  • IDE: Integrated Development Environment. The application that provides the unified user interface for the various development tools including the compiler, debugger, code editor, and designers.
  • Immutable type: A type whose instance data, fields and properties, does not change after the instance is created. Most value types are immutable.
  • Inaccessible member: A member that cannot be accessed by a given type. An inaccessible member for one type is not necessarily inaccessible to another type.
  • Inheritance: C# supports inheritance, so a class that derives from another class, known as the base class, inherits the same methods and properties. Inheritance involves base classes and derived classes.
  • Interface: A type that contains only the signatures of public methods, events, and delegates. An object that inherits the interface must implement all of the methods and events defined in the interface. Classes or structs may inherit any number of interfaces.
  • Iterator: An iterator is a method that enables consumers of a class that contains a collection or array to use foreach, in (C# Reference) to iterate through that collection or array.
  • Member: A field, property, method, or event declared on a class or struct.
  • Method: A named code block that provides behavior for a class or struct.
  • Mutable type: A type whose instance data, fields and properties, can be changed after the instance is created. Most Reference Types are mutable.
  • Nested type: A type declared within the declaration of another type.
  • Object: An instance of a class. An object exists in memory, and has data and methods that act on the data.
  • Property: A data member accessed by means of an accessor.
  • Refactoring: Reusing previously entered code. The Visual C# Express Code Editor can intelligently reformat code to, for example, turn a block of highlight code into a method.Reference type: A data type. A variable declared as a reference type point to a location where data is stored.
  • Static: A class or method declared as static exists without first being instantiated using the keyword new. Main() is a static method.
  • Struct: A compound data type that is typically used to contain a few variables that have some logical relationship. Structs can also contain methods and events. Structs do not support inheritance but they do support interfaces. A struct is a value type, while a class is a reference type.
  • Value type: A value type is a data type that is allocated on the stack, as opposed to a reference type which is allocated on the heap. The built-In types, including the numeric types as well as the struct type and the nullable type, are all value types. The class type and string type are reference types.
Posted by: Amit Andharia | January 2, 2009

How to get properties of an object?

 

I was inspired to write this post because I tried a lot to find this out. During debugging I needed it badly. There are many undocumented properties, that you can’t just alert. Sometimes, even you are in need of finding out a property/attribute of the object that accomplish your need. The code snippet below will list all the properties of any object with type. I’m sure this will be quite helpful to others too.

function ListAttributes(obj,debuggerId){

var result = “”;
for (var i in obj)

result += “obj.” + i + ” = ” + obj[i] + “”;

document.getElementById(debuggerId).innerHTML=result;

}

Parameter List:

Obj = object to be reviewed
debuggerId = Id of the div or any other element, insider which you need to write the complete list of all attributes

Posted by: Amit Andharia | January 2, 2009

Difference between Session.Abandon & Session.Clear

Session.Abandon()

The Abandon method destroys all the objects stored in a Session object and releases their resources. If you do not call the Abandon method explicitly, the server destroys these objects when the session times out.

When the Abandon method is called, the current Session object is queued for deletion but is not actually deleted until all of the script commands on the current page have been processed. This means that you can access variables stored in the Session object on the same page as the call to the Abandon method but not in any subsequent Web pages.

For example, in the following script, the third line prints the value Mary. This is because the Session object is not destroyed until the server has finished processing the script.

<%
Session.Abandon
Session(“MyName”) = “Mary”
Reponse.Write(Session(“MyName”))
%>

If you access the variable MyName on a subsequent Web page, it is empty. This is because MyName was destroyed with the previous Session object when the page containing the previous example finished processing.
The server creates a new Session object ( with a new SessionID) when you open a subsequent Web page, after abandoning a session. You can store variables and objects in this new Session object.

Session.Clear()

The Clear method removes all keys and values from the current session. Compare to Abandon method, it doesn’t create the new session, It just make all variables in session to NULL.

Posted by: Amit Andharia | January 2, 2009

How to find .NET Version Programaticallly?

 It’s very simple. Here is the code snippet for the same.

VB.NET

Response.Write(System.Environment.Version.ToString())

C#

Response.Write(System.Environment.Version.ToString());

Posted by: Amit Andharia | January 2, 2009

Why Viewstate? What is Viewstate?

Why Viewstate?

As we know, HTTP is a state less protocol. State less means the protocol couldn’t maintain state between request and response we must have to use some mechanism to accomplish this. (Like query string, cookie, etc.). When a request is sent from “Client Machine” to “Server Machine” based on the request header details, Server handover processing of request to appropriate process on the server. If the same “Client Machine” send subsequent request for the same Page, Server couldn’t recognize that request is from the same machine. He just processes the request, and sends the response regardless of “Client Machine”.

Consider we need to post some text fields on a page using classic ASP. Suppose some validations fails, so the response generated from server will contain on the Page HTML without the data in the text fields, as it doesn’t know the present status of the page at client end. Think about the user who has filled almost 50 fields on the page, and page returns with all blank field and a validation message on top saying no data saved!!!

This was the main reason why Viewstate is introduced – to maintain the state of the page between round-trip.

What is Viewstate?

Viewstate is the ASP.NET state management technique introduced to maintain current page state between round-trip. It maintains state of the fields, using a hidden field with name = “__VIEWSTATE”. It uses base64 encoding to store the state. Whenever the page is posted to the server, view state is also posted back in addition to the posted data. Click here to check more details on how hidden field is posted back to server

Here is the sample of Viewstate stored at the client side.

<input type=”hidden” name=”__VIEWSTATE” value=”dDwyNTA3OTU0NDM7Oz7t5TntzkOUeB0QVV6FT2hvQwtpPw==” />

Important: As Viewstate is posted to the server each time, It should be used very carefully. Bulky Viewstate can result in performance issues. If you don’t want to maintain the Viewstate, include the directive <%@ Page EnableViewState=”false”%> at the top of an .aspx page If you do not want to maintain Viewstate for any control add the attribute EnableViewState=”false” to any control.

Categories