150 likes | 270 Views
The First C# Program. Elements of a C# Program. Exploring the First C# Program. // Namespace Declaration using System; namespace HolaMundo { /// <summary> /// Summary description for Class1. /// </summary> // Program start class class Class1 { /* Main begins program execution
E N D
The First C# Program Elements of a C# Program
Exploring the First C# Program // Namespace Declaration using System; namespace HolaMundo { /// <summary> /// Summary description for Class1. /// </summary> // Program start class class Class1 { /* Main begins program execution The main entry point for the application. */ static void Main(string[] args) { Console.WriteLine("Hola mundo"); } } }
Comentarios Los comentarios sirven dos funciones : • Hace que el código sea más fácil de leer • Internamente documentan que hace los enunciados del programa
Diferentes tipos de comentarios • Comentarios de una solo linea • Comentarios de multiples linea • Comentarios estilo xml ( extensible markup language)
Using directives and namespaces Namespace: • A namespace can be seen as a container for some classes in much the same way that a folder on your file system contains files. • By putting the classes into namespaces we can group related classes together. If two classes have the same name, by placing them in different namespaces, we avoid the risk of name collisions.
Namespaces • It is recommended that you define all your classes in a namespace. • Every single class in the .net Framework lives inside a namespace. • .net provides the significant benefit of making available more than 2,000 classes that make up what is called the Framework Class Library(FCL). • A class contains code that can be reused, which makes programming easier and faster because you don’t have to reinvent the wheel.
namespaces • The Console class lives inside the System namespace. • This means that its fully qualified name is actually System.Console. • The using directive allows making the names of the classes inside the namespace visible outside the namespace in their shorthand form. • Ver ejemple con la clase Console
Forma general de un programa using System; namespace MyNamespace1 { class MyClass1 { } struct MyStruct { } interface IMyInterface { } delegate int MyDelegate(); enum MyEnum { } namespace MyNamespace2 { } class MyClass2 { public static void Main(string[] args) { } } }
Namespaces • C# Language programs can consist of one or more files. Each file can contain one or more namespaces. A namespace can contain types such as classes, structs, interfaces, enumerations, and delegates, in addition to other namespaces.
Classes • C# is an object-oriented language, everything in C# is designed around a class • C# doesn’t allow anything to be defined outside of a class. • All methods must be defined inside a class.
Classes • Programs written using C# can use these predefined .NET classes or create their own classes. • It is tradition to name the file conaining the class the same name as the class neme, except the file name will have a .cs extention.
Classes • Console is a built-in class that contains the methods for displaying messages on the screen and getting input from the keyboard. • Within the curly braces, you define the class members. A class member is generally either a member method, which performs some behavior of the class, or a data member, which contais a value associated with the state of the class.
Main method • The Main method is special, it designates the program’s entry point. It must be a static method. • This is where the program begins execution
methods • A collection of one or more statement combined to perform an acton. • Methods are similar to functions in C++. • Signature and heading
Method body- Statements • Console.WriteLine(“Hola Mundo”); • Console.Write(“ “); • Console.Write(“What goes\nup\nmust come\tdown”);