没有合适的资源?快使用搜索试试~ 我知道了~
CSharp - Module 5_Methods and Parameters
需积分: 0 3 下载量 197 浏览量
2008-08-24
11:00:52
上传
评论
收藏 1.09MB PDF 举报
温馨提示
试读
54页
CSharp - Module 5_Methods and Parameters
资源详情
资源评论
资源推荐
Contents
Overview 1
Using Methods 2
Using Parameters 16
Using Overloaded Methods 30
Lab 5: Creating and Using Methods 38
Review 50
Module 5: Methods and
Parameters
This course is based on the prerelease Beta 1 version of Microsoft
®
Visual Studio .NET.
Content in the final release of the course may be different from the content included in
this prerelease version. All labs in the course are to be completed with the Beta 1
version of Visual Studio .NET.
Information in this document is subject to change without notice. The names of companies,
products, people, characters, and/or data mentioned herein are fictitious and are in no way intended
to represent any real individual, company, product, or event, unless otherwise noted. Complying
with all applicable copyright laws is the responsibility of the user. No part of this document may
be reproduced or transmitted in any form or by any means, electronic or mechanical, for any
purpose, without the express written permission of Microsoft Corporation. If, however, your only
means of access is electronic, permission to print one copy is hereby granted.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual
property rights covering subject matter in this document. Except as expressly provided in any
written license agreement from Microsoft, the furnishing of this document does not give you any
license to these patents, trademarks, copyrights, or other intellectual property.
2001 Microsoft Corporation. All rights reserved.
Microsoft, ActiveX, BizTalk, IntelliSense, JScript, Microsoft Press, MSDN, PowerPoint, Visual
Basic, Visual C++, Visual C#, Visual Studio, Windows, Windows NT, and Windows Media are
either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other
countries.
Other product and company names mentioned herein may be the trademarks of their respective
owners.
Module 5: Methods and Parameters 1
Overview
n Using Methods
n Using Parameters
n Using Overloaded Methods
In designing most applications, you divide the application into functional units.
This is a central principle of application design because small sections of code
are easier to understand, design, develop, and debug. Dividing the application
into functional units also allows you to reuse functional components throughout
the application.
In C#, you structure your application into classes that contain named blocks of
code; these are called methods. A method is a member of a class that performs
an action or computes a value.
After completing this module, you will be able to:
n Create static methods that accept parameters and return values.
n Pass parameters to methods in different ways.
n Declare and use overloaded methods.
2 Module 5: Methods and Parameters
u Using Methods
n Defining Methods
n Calling Methods
n Using the return Statement
n Using Local Variables
n Returning Values
In this section, you will learn how to use methods in C#. Methods are important
mechanisms for struc turing program code. You will learn how to create
methods and how to call them from within a single class and from one class to
another.
You will learn how to use local variables, as well as how to allocate and
destroy them.
You will also learn how to return a value from a method, and how to use
parameters to transfer data into and out of a method.
Module 5: Methods and Parameters 3
Defining Methods
n Main Is a Method
l Use the same syntax for defining your own methods
using System;
class ExampleClass
{
static void ExampleMethod( )
{
Console.WriteLine("Example method");
}
static void Main( )
{
// ...
}
}
using System;
class ExampleClass
{
static void ExampleMethod( )
{
Console.WriteLine("Example method");
}
static void Main( )
{
// ...
}
}
A method is group of C# statements that have been brought together and given
a name. Most modern programming languages have a similar concept; you can
think of a method as being like a function, a subroutine, a procedure or a
subprogram.
Examples of Methods
The code on the slide contains three methods:
n The Main method
n The WriteLine method
n The ExampleMethod method
The Main method is the entry point of the application. The WriteLine method
is part of the Microsoft® .NET Framework. It can be called from within your
program. The WriteLine method is a static method of the class
System.Console. The ExampleMethod method belongs to ExampleClass.
This method calls the WriteLine method.
In C#, all methods belong to a class. This is unlike programming languages
such as C, C++, and Microsoft Visual Basic®, which allow global subroutines
and functions.
剩余53页未读,继续阅读
StanfordZhang
- 粉丝: 39
- 资源: 235
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0