Working with Visual Studio.Net || Versions of Visual

Working with Visual Studio.Net :

-It was an editor used for developing .Net app in any .net Language like C#, VB etc., as well as any type of app like Console, Windows, Web etc.

-Versions of Visual Studio.Net:
         2002        (Framework 1.0)
         2003        (Framework 1.1)
         2005        (Framework 2.0)
         2008        (Framework 3.5)
         2010         (Framework 4.0)  
         2011 Beta  (Framework 4.5)

-To open VS go to Start Menu -> Programs -> MS Visual Studio -> MS Visual Studio & click on it to open.

-Applications developed under VS are known as projects, where a project is collection of various files or items. To create a Project either click on "Create Project" option or go to File Menu & select New -> Project which open's the "New Project" window.

-Under new project window we need to specify the following details:

1. Under "Project Types" choose the language in which we want to develop the app. eg: Visual C#.

2. Under "Templates" specify the type of app we want to develop by choosing a project template.
eg: Console Application.

3. On top right corner we are given an option to  change the framework version in which we want to develop the app, default is 3.5 in 2008 & 4.0 in 2010 can be changed either to 2.0 or 3.0 also.

4. In the bottom under Name specify a name to the project. eg: SampleProject.

5. Below project name specify the location where to save the project. eg: C:\CSharp9.

-Click on OK button which creates the project with a defualt class Program under the file Program.cs.

-When projects are developed in VS by default a Namespace gets created with same name of the project i.e. SampleProject, from now each & every class of the project comes under same Namespace. We are already aware a NameSpace is collection of types that was Logical.

-Now under Main method of Program class write the following code:

     Console.WriteLine("My first project");

-To run the program either press F5 or Ctrl + F5 or click on start debugging button on top of the studio which will save, compile and then executes the program.

-Under VS we find a window known as Solution Explorer used for organizing the complete app, which allows us to view, add or delete items under the projects. To open Solution Explorer goto View Menu & select Solution Explorer.

-To add a new class under project open Solution Explorer -> right click on project -> select Add -> New Item which opens "Add New Item" window -> select class "Template" in it & specify a name in the bottom -> click on Add button, which adds the class under project. eg: Class1.cs.

Note: the new class added, also comes under the same Namespace.

-Now under the class write following code:

static void Main()
   Console.WriteLine("Second Class");

-Now to run the class open Solution Explorer right click on the project -> select properties -> which opens project property window, under it we find a property StartUp Object which will list all classes under project that contains a valid main method in them, choose your class and run.
Project or Application Management:
-While developing an app some times code may be  written under more than 1 project, where collection of multiple projects is known as Solution.

-When ever we create a new project by default VS will create a Solution and under it the project gets created, where a solution is collection of projects & project is collection of files or items.
         -Collection of Projects
               -Collection of files Or Items

-A Solution also requires a name which by default will have the same name of project, but can be changed if required.

-A solution can have projects of different .net langs as well as of diff. project templates like Windows, Console etc.

-To Add a new project under an existing solution, right click on solution node in Solution Explorer & select add "New Project" which opens New Project window, under that select the language as C#, then select a template of Console Application, name it as 'SecondProject' & then click ok to add project under the solution.

-By default the project contains a class Program under SecondProject namespace, write following code in it's main method:
    Console.WriteLine("Second Project");

-If we want to run the above class, we need to first set a property "StartUp Project", because there are multiple proj's under the solution & VS by default runs first project of the Solution i.e. OOPSProject. To set the 'StartUp Project' property goto Solution Explorer, right click on SecondProject, select "Set as StartUp Project" & then run the project.

Note: if the project contains multiple classes in it to run a class we need to again set StartUp Object property under Second Projects property window.

-The application what we have created gets saved physical on the hard disk in same hierarchy what we see under solution explorer i.e. first it creates a folder referring to solution & under that it creates a seperate folder referring to each project & under that files corresponding to project gets saved.


Note: a solution gets saved with .sln extension & a C# project gets saved with .csproj extension.

-When ever a project is compiled it generates an output file that contains IL Code of all items in the project known as an Assembly. These Assemblies are what we carry on to the client machines when the app has to be deployed, so they are referred as units of deployment.

-The name of assembly file will be same as project name & it will be present under bin\Debug folder of the projects folder.
Q. Can we consume classes of a project from the classes of same project?
Ans: Yes, we can consume them directly because all those classes were under same project and are considered as a family.

Q. Can we consume classes of a project from the classes of other projects?
Ans: Yes, we can consume but not directly as they were under different projects, first we need to add the reference of assembly in which the classes were present to the project who want's to consume them.

Q. How to add reference of an assembly to a proj ?
Ans: To add reference of an assembly to a project open solution explorer -> right click on the project to whom reference has to be added -> select "Add Reference" -> select "Browse" tab on top of the  window which has been opened,
-> then select the assembly we want to consume from its physical location & click ok. Now we can consume classes of the assembly referring with their namespace.
-Access Specifiers: these are also modifiers used to define scope of a type (class or interface or structure etc.) as well as its members i.e. who can access them and who cannot. C# has 5 different access specifiers, those are:
    -protected internal

Note: members defined under the class with any scope are always accessible with in the class, all the restrictions comes into picture only when we try to access them outside of the class.

Private: members declared as private under class & structure are not accessible outside of them in which they are defined. Default scope for members of a class & structure in C# is private & public in case of interface.
 A type under namespace cannot be declared as private.

Protected: members declared as protected under a class can be accessed only from child classes, a non-child class cannot consume them. Classes under a namespace can't be declared as protected.

Q. How to restrict a class not to be accessible for any other classes?
Ans: This can be done by declaring constructors of class as private.

Q. How to restrict a class not to be inherited by any other class?
Ans:This can be done by declaring class as sealed.

Q. How to restrict a class not to be accessible for any other class to consume by creating its object ?

Ans: This can be done by declaring constructors of the class as protected.

Internal: members or types (Class & Structure) that are declared as internal were accessible only with in the project both from child or non-child classes. The default scope for a type in C# is internal only.

Protected Internal: members declared as protected internal enjoy dual scope i.e. with in the project they behave as internal providing access to all other classes, out-side the project they change to protected & still provide access to child classes.

Public: a type or member of a type if declared as public is global that can be accessed from any where.

Case 1: Accessing members with in the same class
Case 2: Accessing members with in child class of             same project.
Case 3: Accessing members with in non-child class     of same project.
Case 4: Accessing members with in child class of             other project.
Case 5: Accessing members with in non-child class     of other project.


Post a Comment