If an assembly contains only one name space, they should use the same name. Otherwise, Assembles should follow the normal Pascal Case format.
All IT and Non IT Interview Questions, MCQs, Online Quiz Questions, Engineering VIVA Questions
Home » All posts
Wednesday, 4 January 2012
Conventions
Namespace:
Namespaces are named using Pascal Case with no underscores. This means the first letter of every word in the name is capitalized. For example: MyNewNamespace. Also, note that Pascal Case also denotes that acronyms of three or more letters should only have the first letter capitalized (MyXmlNamespace instead of MyXMLNamespace)
Namespaces are named using Pascal Case with no underscores. This means the first letter of every word in the name is capitalized. For example: MyNewNamespace. Also, note that Pascal Case also denotes that acronyms of three or more letters should only have the first letter capitalized (MyXmlNamespace instead of MyXMLNamespace)
Tuesday, 3 January 2012
Naming
Naming
This section will define the naming conventions that are generally accepted by the C# development community. Some companies may define naming conventions that differ from this, but that is done on an individual basis and is generally discouraged. Some of the objects discussed in this section may be beyond the reader's knowledge at this point, but this section can be referred back to later.
This section will define the naming conventions that are generally accepted by the C# development community. Some companies may define naming conventions that differ from this, but that is done on an individual basis and is generally discouraged. Some of the objects discussed in this section may be beyond the reader's knowledge at this point, but this section can be referred back to later.
Windows Forms
The System.Windows.Forms namespace allows us to create Windows applications easily. The Form class is a particularly important part of that namespace because the form is the key graphical building block ofWindows applications. It provides the visual frame that holds buttons, menus, icons, and title bars together. Integrated development environments (IDEs) like Visual C# and SharpDevelop can help create graphical applications, but it is important to know how to do so manually:
using System.Windows.Forms;
public class ExampleForm : Form // inherits from System.Windows.Forms.Form
{
public static void Main()
{
ExampleForm wikibooksForm = new ExampleForm();
wikibooksForm.Text = "I Love Wikibooks";// specify title of the form
wikibooksForm.Width = 400; // width of the window in pixels
wikibooksForm.Height = 300; // height in pixels
Application.Run(wikibooksForm); // display the form
}
}
The example above creates a simple Window with the text "I Love Wikibooks" in the title bar. Custom form classes like the example above inherit from the System.Windows.Forms.Form class. Setting any of the properties Text, Width, and Height is optional. Your program will compile and run successfully if you comment these lines out, but they allow us to add extra control to our form.
using System.Windows.Forms;
public class ExampleForm : Form // inherits from System.Windows.Forms.Form
{
public static void Main()
{
ExampleForm wikibooksForm = new ExampleForm();
wikibooksForm.Text = "I Love Wikibooks";// specify title of the form
wikibooksForm.Width = 400; // width of the window in pixels
wikibooksForm.Height = 300; // height in pixels
Application.Run(wikibooksForm); // display the form
}
}
The example above creates a simple Window with the text "I Love Wikibooks" in the title bar. Custom form classes like the example above inherit from the System.Windows.Forms.Form class. Setting any of the properties Text, Width, and Height is optional. Your program will compile and run successfully if you comment these lines out, but they allow us to add extra control to our form.
Console Programming
Console Programming
Output
The example program below shows a couple of ways to output text:
using System;
public class HelloWorld
{
public static void Main()
{
Console.WriteLine("Hello World!"); // relies on "using
System;"
Console.Write("This is");
Console.Write("... my first program!\n");
System.Console.WriteLine("Goodbye World!"); // no "using" statement
required
}
}
The above code displays the following text:
Hello World!
This is... my first program!
Goodbye World!
That text is output using the System.Console class. The using statement at the top allows the compiler to find the Console class without specifying the System namespace each time it is used.
The middle lines use the Write() method, which does not automatically create a new line. To specify a new line, we can use the sequence backslash-n ( \n ). If for whatever reason we wanted to really show the \n character instead, we add a second backslash ( \\n ). The backslash is known as the escape character in C# because it is not treated as a normal character, but allows us to encode certain special characters (like a new line character).
Input
Input can be gathered in a similar method to outputing data using the Read() and ReadLine methods of that same System.Console class:
using System;
public class ExampleClass
{
public static void Main()
{
Console.WriteLine("Greetings! What is your name?");
Console.Write("My name is: ");
string name = Console.ReadLine();
Console.WriteLine("Nice to meet you, " + name);
Console.Read();
}
}
The above program requests the user's name and displays it back. The final Console.Read() waits for the user to enter a key before exiting the program.
to write to the Console.Error stream whenever an error occurred, you can tell your scheduler program to monitor this stream, and feedback any information that is sent to it. Instead of the Console appearing with the Error messages, your program may wish to log these to a file.
You may wish to revisit this after studying Streams and after learning about the Process class.
Custom console applications can have arguments as well.
using System;
public class ExampleClass
{
public static void Main(string[] args)
{
Console.WriteLine("First Name: " + args[0]);
Console.WriteLine("Last Name: " + args[1]);
Console.Read();
}
}
If the program above code is compiled to a program called username.exe, it can be executed from the command line using two arguments,
e.g. "Bill" and "Gates":
C:\>username.exe Bill Gates
Notice how the Main() method above has a string array parameter. The program assumes that there will be two arguments. That assumption makes the program unsafe. If it is run without the expected number of command line arguments, it will crash when it attempts to access the missing argument. To make the program more robust, we make we can check to see if the user entered all the required arguments.
using System;
public class Test
{
public static void Main(string[] args)
{
if(args.Length >= 1)
Console.WriteLine(args[0]);
if(args.Length >= 2)
Console.WriteLine(args[1]);
}
}
Try running the program with only entering your first name or no name at all. The string.Length property returns the total number of arguments.
If no arguments are given, it will return zero.
You are also able to group a single argument together by using the "" quote marks. This is particularly useful if you are expecting many parameters,
but there is a requirement for including spaces (e.g. file locations, file names, full names etc)
using System;
class Test
{
public static void Main(string[] args)
{
for(int index =0 ;index < args.Length; index++)
{
Console.WriteLine((index+1) + ": " + args[index]);
}
}
}
C:\> Test.exe Separate words "grouped together"
1: Separate
2: words
3: grouped together
Output
The example program below shows a couple of ways to output text:
using System;
public class HelloWorld
{
public static void Main()
{
Console.WriteLine("Hello World!"); // relies on "using
System;"
Console.Write("This is");
Console.Write("... my first program!\n");
System.Console.WriteLine("Goodbye World!"); // no "using" statement
required
}
}
The above code displays the following text:
Hello World!
This is... my first program!
Goodbye World!
That text is output using the System.Console class. The using statement at the top allows the compiler to find the Console class without specifying the System namespace each time it is used.
The middle lines use the Write() method, which does not automatically create a new line. To specify a new line, we can use the sequence backslash-n ( \n ). If for whatever reason we wanted to really show the \n character instead, we add a second backslash ( \\n ). The backslash is known as the escape character in C# because it is not treated as a normal character, but allows us to encode certain special characters (like a new line character).
Input
Input can be gathered in a similar method to outputing data using the Read() and ReadLine methods of that same System.Console class:
using System;
public class ExampleClass
{
public static void Main()
{
Console.WriteLine("Greetings! What is your name?");
Console.Write("My name is: ");
string name = Console.ReadLine();
Console.WriteLine("Nice to meet you, " + name);
Console.Read();
}
}
The above program requests the user's name and displays it back. The final Console.Read() waits for the user to enter a key before exiting the program.
Error
The Error output is used to divert error specific messages to the console. To a novice user this may seem fairly pointless, as this achieves the same as Output (as above). If you decide to write an application that runs another application (for example a scheduler), you may wish to monitor the output of that program - more specifically, you may only wish to be notified only of the errors that occur. If you coded your program to write to the Console.Error stream whenever an error occurred, you can tell your scheduler program to monitor this stream, and feedback any information that is sent to it. Instead of the Console appearing with the Error messages, your program may wish to log these to a file.
You may wish to revisit this after studying Streams and after learning about the Process class.
Command line arguments
Command line arguments are values that are passed to a console program before execution. For example, theWindows command prompt includes a copy command that takes two command line arguments. The first argument is the original file and the second is the location or name for the new copy. Custom console applications can have arguments as well.
using System;
public class ExampleClass
{
public static void Main(string[] args)
{
Console.WriteLine("First Name: " + args[0]);
Console.WriteLine("Last Name: " + args[1]);
Console.Read();
}
}
If the program above code is compiled to a program called username.exe, it can be executed from the command line using two arguments,
e.g. "Bill" and "Gates":
C:\>username.exe Bill Gates
Notice how the Main() method above has a string array parameter. The program assumes that there will be two arguments. That assumption makes the program unsafe. If it is run without the expected number of command line arguments, it will crash when it attempts to access the missing argument. To make the program more robust, we make we can check to see if the user entered all the required arguments.
using System;
public class Test
{
public static void Main(string[] args)
{
if(args.Length >= 1)
Console.WriteLine(args[0]);
if(args.Length >= 2)
Console.WriteLine(args[1]);
}
}
Try running the program with only entering your first name or no name at all. The string.Length property returns the total number of arguments.
If no arguments are given, it will return zero.
You are also able to group a single argument together by using the "" quote marks. This is particularly useful if you are expecting many parameters,
but there is a requirement for including spaces (e.g. file locations, file names, full names etc)
using System;
class Test
{
public static void Main(string[] args)
{
for(int index =0 ;index < args.Length; index++)
{
Console.WriteLine((index+1) + ": " + args[index]);
}
}
}
C:\> Test.exe Separate words "grouped together"
1: Separate
2: words
3: grouped together
The .NET Framework
The .NET Framework
.NET Framework Overview
An overview of the .NET class library used in C#.
Console Programming
Input and Output using the console.
Windows Forms
GUI Programming with Windows Forms.
.NET Framework Overview
An overview of the .NET class library used in C#.
Console Programming
Input and Output using the console.
Windows Forms
GUI Programming with Windows Forms.
C Sharp Programming
C# (pronounced "See Sharp") is a multi-purpose computer programming language suitable for all development needs. This WikiBook introduces C# language fundamentals and covers a variety of the base class libraries (BCL) provided by the Microsoft .NET Framework.
Introduction
Main introduction: C Sharp Programming/Foreword Although C# is derived from the C programming language, it has features such as garbage collection that allow beginners to become proficient in C# more quickly than in C or C++. Similar to Java, it is object-oriented, comes with an extensive class library, and supports exception handling, multiple types of polymorphism, and separation of interfaces from implementations. Those features, combined with its powerful development tools, multi-platform support, and generics, make C# a good choice for many types of software development projects: rapid application development projects, projects implemented by individuals or large or small teams, Internet applications,
and projects with strict reliability requirements. Testing frameworks such as NUnit make C# amenable to test-driven development and thus a good language for use with Extreme Programming (XP). Its strong typing helps to prevent many programming errors that are common in weakly typed languages.
Foreword
A description of the C# language and introduction to this Wiki book.
Getting started with C#
A simple C# program and where to get tools to compile it.
Language Basics
Naming conventions
Quickly describes the generally accepted naming conventions for C#.
Basic syntax
Describes the basics in how the applications you write will be interpreted.
Variables
The entities used to store data of various shapes.
Operators
Summarizes the operators, such as the '+' in addition, available in C#.
Data structures
Enumerations, structs, and more.
Control statements
Loops, conditions, and more. How the program flow is controlled.
Exceptions
Responding to errors that can occur.
Classes
Namespaces
Giving your code its own space to live in.
Classes
The blueprints of objects that describes how they should work.
Objects
Cornerstones of any object-oriented programming language, objects are the tools you use to perform work.
Encapsulation and accessor levels
Explains protection of object states by encapsulation.
The .NET Framework
.NET Framework Overview
An overview of the .NET class library used in C#.
Console Programming
Input and Output using the console.
Windows Forms
GUI Programming with Windows Forms.
Advanced Object-Orientation Concepts
Inheritance
Re-using existing code to improve or specialise the functionality of an object.
Interfaces
Define a template, in which to base sub-classes from.
Delegates and Events
Be informed about when an event happens and choose what method to call when it happens with delegates.
Abstract classes
Build partially implemented classes.
Partial classes
Split a class over several files to allow multiple users to develop, but also to stop code generators interfering with source code.
Collections
Effectively manage (add, remove, find, iterate, etc.) large sets of data.
Generics
Allow commonly used collections and classes to appear to have specialisation for your custom class.
Object Lifetime
Learn about the lifetime of objects, where they are allocated and learn about garbage collection.
Design Patterns
Learn commonly used design methodologies to simplify and/or improve your development framework.
Introduction
Main introduction: C Sharp Programming/Foreword Although C# is derived from the C programming language, it has features such as garbage collection that allow beginners to become proficient in C# more quickly than in C or C++. Similar to Java, it is object-oriented, comes with an extensive class library, and supports exception handling, multiple types of polymorphism, and separation of interfaces from implementations. Those features, combined with its powerful development tools, multi-platform support, and generics, make C# a good choice for many types of software development projects: rapid application development projects, projects implemented by individuals or large or small teams, Internet applications,
and projects with strict reliability requirements. Testing frameworks such as NUnit make C# amenable to test-driven development and thus a good language for use with Extreme Programming (XP). Its strong typing helps to prevent many programming errors that are common in weakly typed languages.
Foreword
A description of the C# language and introduction to this Wiki book.
Getting started with C#
A simple C# program and where to get tools to compile it.
Language Basics
Naming conventions
Quickly describes the generally accepted naming conventions for C#.
Basic syntax
Describes the basics in how the applications you write will be interpreted.
Variables
The entities used to store data of various shapes.
Operators
Summarizes the operators, such as the '+' in addition, available in C#.
Data structures
Enumerations, structs, and more.
Control statements
Loops, conditions, and more. How the program flow is controlled.
Exceptions
Responding to errors that can occur.
Classes
Namespaces
Giving your code its own space to live in.
Classes
The blueprints of objects that describes how they should work.
Objects
Cornerstones of any object-oriented programming language, objects are the tools you use to perform work.
Encapsulation and accessor levels
Explains protection of object states by encapsulation.
The .NET Framework
.NET Framework Overview
An overview of the .NET class library used in C#.
Console Programming
Input and Output using the console.
Windows Forms
GUI Programming with Windows Forms.
Advanced Object-Orientation Concepts
Inheritance
Re-using existing code to improve or specialise the functionality of an object.
Interfaces
Define a template, in which to base sub-classes from.
Delegates and Events
Be informed about when an event happens and choose what method to call when it happens with delegates.
Abstract classes
Build partially implemented classes.
Partial classes
Split a class over several files to allow multiple users to develop, but also to stop code generators interfering with source code.
Collections
Effectively manage (add, remove, find, iterate, etc.) large sets of data.
Generics
Allow commonly used collections and classes to appear to have specialisation for your custom class.
Object Lifetime
Learn about the lifetime of objects, where they are allocated and learn about garbage collection.
Design Patterns
Learn commonly used design methodologies to simplify and/or improve your development framework.
Subscribe to:
Comments (Atom)