More Related Content
Similar to Vb.net session 15
Similar to Vb.net session 15 (20)
Vb.net session 15
- 1. Introducing VC#
Objectives
In this lesson, you will learn to:
• Create a console application project in Visual C#
• Create a ‘HelloWorld’ program
• Identify the structure of a VC# program
• Identify the compilation process and output of the VC# program
• Implement various data types in VC#
• Declare variables in VC# code
• Write statements in VC# code
©NIIT Introduction to VB .NET Slide 1 of 41
- 2. Introducing VC#
Introduction to VC#
• VC# is an object-oriented language that enables you to build applications for
the Microsoft .NET platform.
• You can create applications in VC#, which can be used as independent
components for the .NET platform.
• VC#, as a part of the Microsoft Visual Studio .NET, has been developed as an
extension of C++ to provide flexibility and control to a programmer.
©NIIT Introduction to VB .NET Slide 2 of 41
- 3. Introducing VC#
Creating a Console Application Project
in VC#
• The steps to create a new project in VC# are:
• Select the File New Project option in the Visual Studio .NET IDE.
Then the New Project dialog box will be displayed, as shown in the
following figure:
©NIIT Introduction to VB .NET Slide 3 of 41
- 4. Introducing VC#
Creating a Console Application Project
in VC# (Contd.)
• In the New Project dialog box, select VC# Projects from the Project Types
pane and Console Application from the Templates pane.
©NIIT Introduction to VB .NET Slide 4 of 41
- 5. Introducing VC#
Creating a Console Application Project
in VC# (Contd.)
• Type the name of the new project.
• Specify the location where the new project is to be created. You can use the
Browse button to browse to the folder in which the new project is to be
created.
• Click the OK button.
• When you perform these steps, two files will be created in the VC# project
folder. They are:
• assemblyinfo.cs file
• class1.cs file
©NIIT Introduction to VB .NET Slide 5 of 41
- 6. Introducing VC#
Creating a ‘HelloWorld’ Program
• In order to create a ‘HelloWorld’ program in VC#, perform the following
steps:
• Select the View Solution Explorer option.
• In the Solution Explorer window, double-click the “class1.cs” file.
• Add the following code snippet in the Main(string[] args).
Console.WriteLine("Hello, C#.NET World");
©NIIT Introduction to VB .NET Slide 6 of 41
- 7. Introducing VC#
Structure of a VC# Program
• The syntactical differences between VB.NET and VC# codes are listed in the
table below:
©NIIT Introduction to VB .NET Slide 7 of 41
- 9. Introducing VC#
Structure of a VC# Program (Contd.)
• The Using Directive enables you to refer the classes in the namespace
without using a fully qualified name.
• WriteLine() is a method, defined in the class Console, that writes text to
standard output followed by a new line.
• The class Console is contained in the namespace System. The following
code snippet applies the using directive.
using System;
class Class1
{
static void Main(string[] args)
{
Console.WriteLine
("Hello, VC#.NET World!");
}
}
©NIIT Introduction to VB .NET Slide 9 of 41
- 10. Introducing VC#
Structure of a VC# Program (Contd.)
• Class Declaration
• In VC#, you need to encapsulate a function within a class.
• To declare a new class, the class statement is used.
• The Main() Function
• The startup code is added in the Main() function.
• An application starts with the Main() function.
• The Main() method has a parameter, args .
• The parameter, args, is an array that can hold a list of strings.
• The system populates this array with strings following the program
name entered on the command line.
©NIIT Introduction to VB .NET Slide 10 of 41
- 11. Introducing VC#
Compilation Process and Output of
VC# Program
• Select the Build option in the Build menu to compile a VC# project. The
output window displays the error messages from the VC# compiler.
• Select Start Without Debugging option in the Debug menu to run the
Hello World application.
©NIIT Introduction to VB .NET Slide 11 of 41
- 12. Introducing VC#
From the Expert’s Desk
• This section provides:
• Best practices
• Tips and Tricks
• FAQs
©NIIT Introduction to VB .NET Slide 12 of 41
- 13. Introducing VC#
Best Practices
Naming Conventions and Standards in VB.NET and
VC#
• Use Pascal casing for class and method names. In Pascal casing, the
class and method names have the first character of all words are in upper-
case and the other characters are in lower-case. For example:
public class HelloWorld
{
void GetHello(string name)
{
......
}
}
©NIIT Introduction to VB .NET Slide 13 of 41
- 14. Introducing VC#
Best Practices
Naming Conventions and Standards in VB.NET and
VC# (Contd.)
• Use the Camel casing for variable names and method parameters. In Camel
casing, the first character of all words, except the first character, are in
upper case and the other characters are in lower-case. For example:
public class HelloWorld
{
int intCount = 0;
void GetHello(string name)
{
string someMessage = "Hello " + name;
...
}
}
©NIIT Introduction to VB .NET Slide 14 of 41
- 15. Introducing VC#
Best Practices
Good Programming
• Avoid large files while programming. If the codes exceed 300-400 lines then
it is better to divide the codes into smaller classes.
• Avoid long methods while writing codes. On an average, a method should
not have more than 25 lines of code. The longer methods should be divided
into smaller chunks.
• Method names should be specific to the task it is suppose to perform.
Good Practice:
void SaveIdentityNumber ( string identityNumber )
{
// Save the identity number.
}
©NIIT Introduction to VB .NET Slide 15 of 41
- 16. Introducing VC#
Best Practices
Tips and Tricks
• Formatting Controls on a Form in VB.NET
• You can format controls in VB.NET by performing the following steps:
• Aligning Controls on a Windows Form
• Standardizing size of Controls
• Manipulating horizontal spaces and vertical spaces between
Controls
©NIIT Introduction to VB .NET Slide 16 of 41
- 17. Introducing VC#
FAQs
• What are the basic system requirements for programming in VC#?
Processor
• 450 MHz Pentium II-class processor and above
Operating System
• Windows XP professional
• Windows XP Home Edition
• Windows 2000 Professional (SP3 or above)
• Windows 2000 Server (SP3 or above)
• Microsoft Windows Server 2003
Memory
• 160 MB of RAM or above
Hard Disk
• 1.5 GB for installation
• 2 GB for MSDN Library documentation
• 500 MB of space while working
©NIIT Introduction to VB .NET Slide 17 of 41
- 18. Introducing VC#
FAQs (Contd.)
Display
• Super VGA or above
Others
• NET framework software development kit
• Editor to write source code
• How do I get the .NET runtime?
• You can get the .NET runtime on the Microsoft MSDN site:
http://msdn.microsoft.com/netframework/technologyinfo/howtoget/d
efault.aspx
• You can also now get the framework via the Windows Update function
of the Start menu.
.NET also comes with Windows Server 2003, and Office 2003.
©NIIT Introduction to VB .NET Slide 18 of 41
- 19. Introducing VC#
FAQs (Contd.)
• Can null be assigned to local variables?
The Just-In-Time (JIT) compiler converts the intermediate code to native
code so that it can execute on the target operating system. JIT keeps a
track of lifetime of variables and analyzes when there is no need of that
variable. Assigning null increases the lifetime of a variable.
©NIIT Introduction to VB .NET Slide 19 of 41
- 21. Introducing VC#
Challenge (Contd.)
• Across:
• (Across 5) A model through which security is achieved in .NET
platform.<Abbv.> (3)
• (Across 61) Descriptive data stored in PE along with MSIL. (3)
• (Across 92) A compiler that compiles the portable executable.<Abbv.> (3)
• (Across 103) A single deployable unit that contains all the information about
the implementation of classes, structures, and interfaces. (8)
• Down:
• (Down 2) A directive used to reference classes in the namespace.<Abbv.>
(5)
• (Down 7) An architecture in which a single executable file handles all
functions relating to the user, business, and data service layers. (10)
• (Down 8) Help to create logical groups of related classes and interfaces. (10)
• (Down 30) Feature that allows you to place a window against any of the
edges of the parent window. (7)
• (Down 84) The system that specifies the type supported by CLR.<Abbv.> (3)
©NIIT Introduction to VB .NET Slide 21 of 41
- 23. Introducing VC#
Variables and Statements in VC#
• The object class is the base class of all data types. Data types represent
the characteristics, such as size and range, of the data in memory.
• VC# supports two categories of data types:
• Value Types
• Reference Types
©NIIT Introduction to VB .NET Slide 23 of 41
- 24. Introducing VC#
Programming with Variables and
Statements in VC#(Contd.)
• Value Types are further categorized into two types:
• Bool type
• The bool type represents 16-bit boolean logical values, such
as true and false. The default value of the bool type is false.
• Numeric Types
• Numeric data types are further categorized into two types:
• Integral
• Floating point
• Decimal
©NIIT Introduction to VB .NET Slide 24 of 41
- 25. Introducing VC#
Programming with Variables and
Statements in VC#(Contd.)
• Reference Data Types
• The reference data types contain a reference of a variable of the data
type, such as a class or a string.
• A class type consists of data members, such as fields and constants.
In addition, a class type consists of function members, such as
methods, properties, and operators.
• The string type inherits directly from the object class. You can use
the variables of the string type to store strings.
©NIIT Introduction to VB .NET Slide 25 of 41
- 26. Introducing VC#
Programming with Variables and
Statements in VC#(Contd.)
• Variables
• VC# categorizes variables into three types based on their scope
and availability. The three types of variables are:
• Static variables
• Instance variables
©NIIT Introduction to VB .NET Slide 26 of 41
- 27. Introducing VC#
Programming with Variables and
Statements in VC#(Contd.)
• Statements
• You can use statements to perform conditional execution in a program. C#
provides the following statements:
• if statement
• Executes a block of code based on the value of a boolean expression.
• The goto statement
• Transfers the control to a statement that is identified by a label.
©NIIT Introduction to VB .NET Slide 27 of 41
- 28. Introducing VC#
Programming with Variables and
Statements in VC#(Contd.)
• The switch statement
• Executes the block of code that is associated with the value of the
controlling expression.
• The for statement
• It consists of an initialization expression, a condition, and an
increment statement. The for statement evaluates the initialization
expression. The statement repeatedly executes the code within the
for block until the condition is false.
• The for..each statement
• It checks a condition for each element in an array.
©NIIT Introduction to VB .NET Slide 28 of 41
- 29. Introducing VC#
Programming with Variables and
Statements in VC#(Contd.)
• The while statement
• It conditionally executes the code within the while block zero or more
times.
• the do..while statement
• It conditionally executes the code within the do..while block one or
more times
©NIIT Introduction to VB .NET Slide 29 of 41
- 30. Introducing VC#
From the Expert’s Desk
• This section provides:
• Best Practices
• Tips and Tricks
• FAQs
©NIIT Introduction to VB .NET Slide 30 of 41
- 31. Introducing VC#
Best Practices
Recommended Function Scope
• Whenever a new application is developed, all the methods defined inside the
solution projects should be declared with the private access-modifier. This
helps in developing a robust application. When private access-modifier is
used in a method declaration, no object of the containing class or the
classes derived from the containing class will be able to access the private
method. Whenever there is a need to use a private method in derived
classes for overriding, the private modifier can be replaced by protected
modifier. If there is a need to call the private methods through objects of
other classes, the private or protected modifier can be replaced by public
modifier.
©NIIT Introduction to VB .NET Slide 31 of 41
- 32. Introducing VC#
Best Practices(Contd.)
Use Inheritance For Extensibility And
Maintainability
• You should try to create new forms and controls by inheriting through the
existing forms and controls of the project. This helps in debugging the
applications. Consider the example of form inheritance. It is better to create
a base form that has items such as logos, command buttons, menus etc.
The new forms can now inherit from this form. It will help in developing
applications quickly. In addition, it will increase the maintainability of the
applications because the amount of code the programmer needs to write will
be reduced. You should also write small codes in the functions and
procedures.
©NIIT Introduction to VB .NET Slide 32 of 41
- 33. Introducing VC#
Tips and Tricks
Tips on Shortcut Menus in VC#
• The Toolbox (Ctrl+Alt+X) window has multiple tabs. You can drag
and drop code onto this window and copy it anywhere. Some tabs do
not allow dropping code into them. To identify the tabs that allow
dropping code into them can be identified through a unique icon. The
General tab has the icon.
• Ctrl+- (Ctrl + Hyphen) is used to navigate through the code positions
you visited recently. Ctrl+Shift+- is used to navigate in the opposite
direction. Floating point
• Matching brace / comment / region / quote works in VC# only. Ctrl+]
is used to find a matching brace. It can also be used to find a
matching comment, region or quote depending upon what is pointed
by the cursor currently.
©NIIT Introduction to VB .NET Slide 33 of 41
- 34. Introducing VC#
Tips and Tricks (Contd.)
Tips on Shortcut Menus in VC#
• Press Alt and then select the area you want to copy with your mouse.
• Build and Debug shortcut keys:
• Build - Ctrl+Shift+B
• Run - Ctrl+F5
• Debug – F5
• Navigate through build errors - F8
©NIIT Introduction to VB .NET Slide 34 of 41
- 35. Introducing VC#
FAQs
• Can static and instance methods be declared with the same name?
Yes, static and instance methods be declared with the same name.
Whenever the method will be invoked through the containing class, the
static version of the method will be executed. In contrast, whenever the
method will be invoked through an object of the class containing the
methods, the instance version of the method will be executed
• State the difference between override and new modifiers?
Override modifier enables a base class method to hide the derived class
implementation of the method. In contrast, the new modifier enables a
derived class method to hide the base class implementation of the method.
©NIIT Introduction to VB .NET Slide 35 of 41
- 36. Introducing VC#
FAQs (Contd.)
• What is the difference between managed code and unmanaged code?
Managed code is the code that is managed by the CLR. In other words,
managed code is written to target the CLR. All code written in Visual Basic
and C# is managed by default. Unmanaged code, on the other hand, does
not benefit from the features provided by the CLR, such as garbage
collection and type safety.
• Is it possible to monitor the activities and performance of the CLR?
Yes. You can use Perfmon.exe in Windows 2000 to monitor the activities
and performance of the CLR. Expand the Performance Logs and Alerts
node. Click on Counter Logs. Double-click System Overview. In the
System Overview Properties dialog box, click the Add button. In the
Select Counters dialog box, select the Performance object drop-down
list. This list displays various objects, such as .NET CLR Interop and .NET
CLR Memory. You can add these objects and monitor the activities and
performance of the CLR.
©NIIT Introduction to VB .NET Slide 36 of 41
- 38. Introducing VC#
Challenge (Contd.)
• Across:
• (Across 2) Which formal parameter in VC# is declared without any
modifier? (5)
• (Across 22) Which access-modifier restrict the methods to be
referenced inside the program only? (9)
• (Across 42) Which modifier is used with parameter arrays in the
formal parameter list of a method declaration? (6)
• (Across 112) Which methods are called through class names? (6)
• Down:
• (Down 1) The method body of an external method simply consists of?
(9)
• (Down 2) Name the keyword used as return-type of a method that
does not return anything. (4)
©NIIT Introduction to VB .NET Slide 38 of 41
- 39. Introducing VC#
Challenge (Contd.)
• (Down 6) Name the method-modifier which enables language
interoperability. (6)
• (Down 8) Which modifier for a method prevents a derived class from
further overriding a base class function in VC#? (6)
• (Down 14) What do you call a user-defined declarative information
attached to class member declarations? (9)
• (Down 42) Which modifier cannot be used if virtual, override, or
abstract modifiers are being used? (7)
• (Down 43) Which modifier cannot be used if the virtual modifier is
used for a method? (8)
• (Down 45) What is the name given to the arguments passed in a
method call?
• (Down 67) Which access-modifier enables methods to be referenced
outside the class through objects? (6)
©NIIT Introduction to VB .NET Slide 39 of 41
- 41. Introducing VC#
Summary
• VC# is an object-oriented language that enables you to build applications for
the Microsoft .NET platform.
• You can create applications in VC#, which can be used as independent
components for the .NET platform
• VC#, as a part of the Microsoft Visual Studio .NET, has been developed as
an extension of C++ to provide flexibility and control to a programmer.
However, VC# shares the same semantics as C++ for creating applications.
• There are some syntactical differences between VB.NET and VC# codes
• The object class is the base class of all data types. Data types represent the
characteristics, such as size and range, of the data in memory.
• VC# supports two categories of data types:
• Value Types
• Reference Types
©NIIT Introduction to VB .NET Slide 41 of 41