Session 11-12 switch statement in C#
switch statement
break statement if break statement is used inside a switch statement,he control will leave the switch statement
goto statement you can either jump to another case statement,or to specific label(label is some Word you coding to mark).
Warning:Using goto is bad PRogramming style,we can should avoid go to by all means
Multiple if else statements can be replaced with a switch statement
for an excample:
if(number ==10)
{Console.WriteLine("you number is 10");}
else if(number ==20)
{Console.WriteLine("you number is 20");}
else if(number ==30)
{Console.WriteLine("you number is 30");}
else
{Console.WriteLine("you number is not 10 to 30");}
above code can be replaced as:
switch(number)
{
case :10
Console.WriteLine("you number is 10");
break;
case :20
Console.WriteLine("you number is 20");
break;
case :30
Console.WriteLine("you number is 30");
break;
defalut:
Console.WriteLine("you number is not 10 to 30");
}
or can be replaced as:
switch(number)
{
case:10
case:20
case:30
Console.WriteLine("you number is{0}",number);
default:
Console.WriteLine("you number is not 10 to 30");
}
Noted that switch(expression) , expression value must be bool , char, string, int ,enum or nullable type.
Session 13 while loop in c#
1,while loop checks the condition first.
2,if the condition is true,statements with in the loop are executed.
3,this process is repeated as long as the condition evaluetes to true.
Note:Don't forget to update the variable participating in the condition,so the loop can end , at some point
Session 14 do while loop in c#
1,a do loop checks its condition at the end of the loop.
2,this means that the do loop is guranteed to execute at least one time.
3,do loops are used to present a menu to the user
Difference -while & do while
1,while loop checks the condition at the beginning, where as do while loop checks the condition at the end of the loop.
2,do loop is guaranteed to execute at least once, where as while loop is not
Session 15for and foreach loops in c#
The for loop
A for loop is very similar to while loop. in a while loop we do the initialization at one place, condition check at another place, and modifying the variable at another place,where as for loop has all of these at one place.
foreach loop
A foreach loop is used to iterate through the items in a collection.For example,foreach loop can be used with arrays or collectioins such as ArrayList, HashTable and Generics.
Session 16Methods in c#
Static Vs Instance methods
static method is invoked using the class name, where are an instance method is invoked using an instance of the class.
the difference between instance methods and static methods is that multiple instances of a class can be created(or instantiated) and each instance has its own separate method. however, when a method is static,there are no instances of that method, and you can invoke only that one definition of the static method
Session 17Method parameters
The 4 types of method parameters
1,value parameters
2,reference parameters(use ref keyword)
3,out parameters(use out keyword)
4,parameter arrays(use params keyword ,should be the last one int the parameters list,and only have one params paremeter.)
method parameters Vs method arguments
session 18 namespace
namespaces are used to organize your programs
they also provide assistance(帮助) in avoiding(避免) name clashes(冲突)
namespaces don't correspond(对应) to file, directory(目录) or assembly(程序集) names. they could be wirtten in separate(单独) files and/or separate assemblies and still belong to the same namespace.
Namespaces can be nested(嵌套) in 2 ways.
namespace alias directives(指令). sometimes you may encounter(遇到) a long namespace and wish to have it shorter. this could improve(提高) readability(可读性) and still avoid(避免) name clashes(冲突) with similarly named methods.
if the name of namespace is to long,you can use 'name'='namespace' to improve readability.
eg:
before: using system.subsystem.hello
after: using ssh = system.subsystem.hello
session 19Introduction to classes
what is a class
a class consists of(由..组成) data and behavior. class data is represented(代表) by it's fields and behavior is represented by it's methods
Purpose of a class constructor
The purpose of a class constructor is to initialize class fields. A class constructor is automatically called when an instance(实例) of a class is created.
Constructors do not have return values and always have the same name as the class.
Constructors are not mandatory(强制性的). if we do not provide a constructor, a default parameter less constructor is automatically provided.
Constructors can be overloaded by the number and type of parameters.
session 20Static and instance class members
Static class members
Instance class members
Difference between static and instance members
An example explaining when you should make certain members static
Static and Instance class memebers
When a class memeber includes a static modifier, the member is called as static member.
When no static modifier is present the member is called as non static member or instance member.
Static members are invoked using class name, where as instance members are invoked using instances(object) of the class.
An instance member belongs to specific instance(object) of a class. if I create 3 objects of a class, I will have 3 sets of instance members in the memory, where as there will ever be only one copy of a static member, no matter how many instances of a class are created.
Note: Class member = field, methods, properties, events, indexers,constructor.
Static constructor
Static constructors are used to initialize static fields in a class.
you declare a static constructor by using the keyword static in front of the constructor name.
Static Constructor is called only once, on matter how many instances you create.
Static constructors are called before instance constructors.
新闻热点
疑难解答