
- Forum
- Programming Talk
- Microsoft .NET
- C# Keywords
C# Keywords
This is a discussion on C# Keywords within the Microsoft .NET forums, part of the Programming Talk category; Hello Everyone, Lets try to post in an explanation of one C# keyword at a time in this thread. Let ...
-
C# Keywords
Hello Everyone,
Lets try to post in an explanation of one C# keyword at a time in this thread.
Let this be a common thread for all the C# keywords.
Please start a separate thread for VB.NET or other languages keywords, if anyone else wants to do so.
Following are the list of C# keywords and the posts following to this, would try to explain each of these one by one.
abstract
as
base
bool
break
byte
case
catch
char
checked
class
const
continue
decimal
default
delegate
do
double
else
enum
event
explicit
extern
false
finally
fixed
float
for
foreach
goto
if
implicit
in
int
interface
internal
is
lock
long
namespace
new
null
object
operator
out
override
params
private
protected
public
readonly
ref
return
sbyte
sealed
short
sizeof
stackalloc
static
string
struct
switch
this
throw
true
try
typeof
uint
ulong
unchecked
unsafe
ushort
using
virtual
void
volatile
while
Please feel free to join in for explaining any of these keywords listed here.
But, please remember to modify the reply with the keyword listed in it.
Happy Keyword'ing (pun intended
)
-
using keyword
using keyword has two different usages -
1) using could be used as a directive to include a namespace within the class. By doing so, all the types defined within that namespace becomes available to the class where it is being included.
Following is an example of how using is used as a directive to make the types within System and System.Data namespace available to the class:
Including the using directive on top of the class is not mandatory. Lets look at the following examples:Code:using System; using System.Data;
Sample 1:
Sample 2:Code://This sample does not have the using directive namespace MyApplication { class HelloWorld { static void Main() { //Console is defined within the System namespace System.Console.WriteLine("Hello World!!!"); } } }
In Sample 1 above, since we have not made use of the using directive, Console class will have to be referred with its fully qualified name, System.Console. Imagine, if you were referring the Console class multiple times within your class, you will have to always refer it by its fully qualified name. In Sample 2 above, Console may not be referred with its fully qualified name. The class can directly refer to the type within the System namespace.Code://This sample uses the using directive to include the System namespace using System; namespace MyApplication { class HelloWorld { static void Main() { //Console is defined within the System namespace Console.WriteLine("Hello World!!!"); } } }
It could also be used to create an alias for a namespace or class.
Code://Alias for System.Console class using CommandConsole = System.Console; namespace MyApplication { class HelloWorld { static void Main() { //Console is defined within the System namespace CommandConsole.WriteLine("Hello World!!!"); } } }
2) using could also be used as a statement to define the scope of an object, whereby the object will be disposed at the end of the scope.
Many of the resource consuming objects need to be disposed as soon as those the program is done using it. It's the developer's responsibility to make sure that they have called the Dispose method immediately after its usage. Delaying the disposal of an object could have its own impact on performance and resource availability to other consumers.
The following example shows how a Dispose method is called typically.
Imagine, forgetting to call the Dispose method on the fs. The file stream would remain open, the file would not be available to other programs immediately leading to performance issues, etc...Code:System.IO.FileStream fs = new System.IO.FileStream("c:\file.txt", System.IO.FileMode.Open); try { // do something with fs - may be read the contents of the file } finally { fs.Dispose(); }
This is where the using statement comes in handy.
The following sample shows how we can avoid having to write out the finally block or calling the Dispose method explicitly.
You could also create the fs object within the using statement as follows:Code:System.IO.FileStream fs = new System.IO.FileStream("c:\file.txt", System.IO.FileMode.Open); using(fs) { // do something with fs - read contents }
Code:using (System.IO.FileStream fs = new System.IO.FileStream("c:\file.txt", System.IO.FileMode.Open)) { // do something here }
This is all about the using keyword. Hope, you find it helpful.
Please feel free to post in your thoughts, comments, suggestions.
-
class keyword
Let us look at one of the most important keyword class which will allow us to define the characteristics of an object on which the whole concept of Object Oriented Programming is based on.
Simply putting, the class keyword is used to declare a class.
A class could within have properties, methods, events, delegates and even nested classes.
Code:class ClassA { //class members would go here }
A class in C# supports derivation from only one parent class but can implement multiple interfaces
Code:class ClassB : ClassA //ClassB is being derived from ClassA { //members would go here }A class declaration could precede with an access specifier - private, protected, friend or public.Code:class ClassC : IFoo, IBoo //ClassC implements interfaces IFoo and IBoo { //Implementation for IFoo and IBoo interfaces would go here }
-
hello guys i want to make an application that could record the keys pressed on keyboards so can anyone please help me.
-
04-16-2012, 04:07 PM #5
- Join Date
- Apr 2012
- Answers
- 12
I'm not much help yet as I'm not confident to explain any of this for anyone. I'm putting in a request though...
Can someone explain the "switch" keyword for me? That would be a great help.
-
04-17-2012, 04:12 PM #6
- Join Date
- Apr 2012
- Answers
- 8
I hope this is not a "dumb" question but I just wonder which of the above keyword commands in c# are used the most often? I think I've seen, for instance, 'namespace' used quite a bit.
-
Sponsored Ads

Reply With Quote





