C#note 02: Namespaces, Classes, Structs, Constants, Operator overloading, if and switch

Namespaces 

Namespaces are a way of organizing the various types that occur in a c# program. It is somewhat similar in concept to a folder in a computer file system. A C# program contains one or more namespaces and each namespace is either defined by you the programmer, or defined as part of a previously written class library.

For example, the namespace System includes the Console class, a class that contains methods for reading and writing to the console window. The System namespace also contains a number of other namespaces, such as System.IO and System.Collections. The .NET Framework alone has more than eighty namespaces, each with up to thousands of classes: namespaces are used to minimize the confusion that might be caused between similarly named types and methods.

 

Creating Your Own Namespace

It is common to use namespaces when working on large programs. Using your own namespzces provides a degree of control over similarly named methods and types. 

 

Classes 

C# is an object-oriented programming language, and in common with other modern languages, it groups related fields, methods, properties, and events into data structures called classes.

Classes vs. Objects

A class is basically a blueprint for a custom data type. After you define a class, you can create its objects or instances. You create an instance of a class by using the C# keywork new.

Static vs. Instance Members

A static member is a method or field that can be accessed without reference to a particular instance of a class. The most common static method is Main, which is the entry point for all C# programs; note that you do not need to create an instance of the containing class in order to call the Main method. Another commonly used static method is WriteLine in the Console class. Note the difference in syntax when accessing static methods; you use the class name, not the instance name, on the left side of the dot operator: Console.WriteLine.

When you declare a class field static, all instances of that class will "share" that field.

A static class is one whose members are all static. Static classes, methods and fields are useful in certain scenarios for performance and efficiency reasons. However, subtle errors can arise if you assume that a field is an instance field when in fact it is static. 

When designing your program, it is good practice, but not a requirement, to keep a single class in each source code file.

 

Constructors 

Constructors typically set the initial values of variables defined in the class. This is not necessary if the nitial values are to be zero for numberic data types, false for Boolean types, or null for reference types, as these data types are initialized automatically.

You can define constructors with any number of parameters. Constructors that do not have parameters are called default constructors.

 

Destructors 

Because of the automatic garbage collection system in C#, it is not likely that you will ever have to implement a destructor unless your class uses unmanaged resources. 

 

Structs 

A struct is a kind of type that is similar to a class in many ways except that it does not support inheritance. 

 

Structs 

A struct in C# is similar to a class, but it is intended for smaller data structures. A struct is a value type, whereas a class is a reference type. Each struct object contains its own copy of the data, whereas a class object contains only a reference to the data.

A struct types inherit from the Object class, which is a root clas in c#. You cannot inherit from a struct, although a struct can implement interfacws.

A struct typically can be created faster than a class. If in your application new data structures are being created in large numbers, you should consider using a struct instead of a class. Structs are also used to encapsulate groups of data fields such as the coordinates of a point on a grid, or the dimensions of a rectangle. 

 

Constants

A constant is a type of field. It holds a value that is assigned when the program is compiled, and never changes after that. 

A readonly variable is like a constant but its value is assigned when a program starts up. This allows you to set the value based on some other condition that you don't know until the program is running. But after that first assignment, the value cannot change again while the program is running.

 

Operator Overloading

C# supports operator overloading; this allow you to redefine operators to be more meaningful when used with your own data types. 

 

If and Switch

Unlike C and C++, if statements require Boolean values. For example, it is not permissible to have a statement that doesn't get evaluated to a simple True or false, such as (a=10). In C#, 0 cannot be substituted for False and 1, or any other value, for True.

The expression that the switch statement uses to determine the case to execute must use the Built-in Data Types, such as int or string; you cannot use more complex user-defined types.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值