Transcript for:
Basics of C# Programming and Structure

hi welcome to prism technologies my name is Venkat this is part 1 introduction to c-sharp in this module we will understand the basic structure of the c-sharp program what is a names based focus on the main method with that let's jump straight into a demo fire up Visual Studio go to all programs Microsoft Visual Studio 2010 Microsoft Visual Studio 2010 this fires up Visual Studio now create a new project go to file new project and remember with not net we can use a variety of different programming languages like C sharp and there are other programming languages as well Visual Basic C++ F sharp now we will choose a programming language as C sharp and with.net you can create different types of applications you can create web applications windows applications of this application shape or WCF etc but we will be creating a console application so the type of application is consumable application and the programming languages we should see sharp we will name a project as introduction to c-sharp and we are going to create that project and C Drive click OK so this creates a seizure control project for us now we will be talking about solution Explorer properties window toolbox etc in a later session so let's close them from now and I wouldn't make this program as simple as possible so let's leave only the basic required you know lines of code within this program and get rid of the others so let me get rid of this unwanted namespace declarations I also don't require their slime this one and I don't even require these parameters to the main method right so if you look at this particular program it's a very simple program hardly three lines is called couple of places around it okay now what do we want this program to do I want this program to print a very simple message onto a console screen if you're wondering what is a console screen it's nothing but the command prompt window and to get to the command prompt window start run and type CMD or C um a and E okay anything would get you to the command prompt so this is the command okay so on a window like this I want a message welcome to see you should have training and I want you seizure program to do that and to do that it's very simple okay I don't have to write any new methods or something like that I can make use of the classes that are present inside it or not framework now if you're new to what is not a framework that please check you know my video tutorial on net framework basics okay so with internet framework we have a class called console class okay we can use this console class basically to write or read messages from the console window if we use a has type something into that okay so now my program has to write something so I'm going to use a function called write line okay so I'm using this red line function and to that I will pass a message that I want to be printed on to the console for example I want this message will come to C sharp training to be printed on to the message and now to run this program all I have to do is press ctrl f5 and once you do that you have that message on the screen welcome to C sharp training press any key to continue and I do that the program terminates okay so it's as simple as that so if I have to print a message all you have to do is use the straight line method pass it the message that you want to plant it and this function is present inside this class now if you look at this program a couple of pieces involved in this okay the first line right here is called the namespace declaration okay so what do we mean by namespace declaration basically this line tells that to the rest of the program we are going to make use of code that is present inside this system namespace okay so what is the namespace I mean element understand that a namespace is a collection of classes for example okay because this control class if you look at the fully qualified name of that this comes on glass full name is actually system so the control class is present inside the system namespace so if I hover over that it says namespace system so this control class is present inside this namespace if I don't have this declaration there what's going to happen look at this we get an error okay so what is that the name console does not exist in the current context okay so this red squiggly line indicates it's an error so if I try to build this application if I go to the build menu and say spill solution I get a compiler error the name does not exist the current context that's because the program does not know where is this coming from so to let our program know that we are going to making classes that are present inside the system namespace we use the Declaration call using system like so so this will tell to program you know we are going to make use of that class control class which is present inside this namespace if you ask me either namespace just a collection of classes no it's a collection of variety of things for example let me take you to the slide so in this slide you can see that a namespace is basically used to organize you cool and it's a collection of classes interfaces structures enums and delegates now what are these okay so we will be talking about classes interfaces structures enhance and delegates in a very great detail in a later video sessions so until then don't worry so but just understand that a namespace is basically used to organize your code and it's a collection on these okay so that's that now just to show you it's in fact is a collection of all that system dot look at this for example action that's a delegate so a namespace can contain delegates and if you look at this one activator that's a class and similarly there are various other things like events etc and if you look at this interestingly this one is actually a namespace so a namespace can contain other names basis as well so what is the namespace a namespace is used to organize your code and it's a collection of classes delegates namespaces etc okay now is it mandatory for me you for me to use a declaration like this no not really you can get rid of that if you want but if you do that you have to use the fully qualified name like so so you can either use the fully qualified name like that or you can use the namespace declaration it's up to you whichever is convenient usually in general people use namespace declaration at the top because if a new program if you are referring to control class multiple times you don't have to use the system keyword you know every time you use the class if you have a namespace declaration here so that makes it more easy let's type in basically okay alright so that's about namespace declaration another important thing to understand here so let's get rid of this let's have another namespace declaration using system and then the next piece of thing that you have to understand is this class class program okay so any piece of school that you write to actually be inside a class and talk about classes there are static classes and instance classes which we'll be talking about in a very great detail in a later session so until then don't worry we just require a class any piece of code that we write shouldn't be residing inside a class so this function should have a place to live and that is when this class program okay and this method this is the function functions and methods are the one which will actually do the work functions in a very great detail again in a later session a function will have something relaxed modifiers static modifiers written types function names and parameters so we'll be talking about all these in a later session but if you look at right here this function name is called as main function so basically this function tells okay the program execution when I actually run the program the execution should start right there and end right there so if I run this program what's going to happen it enters you know it starts executing here prints this line and terminates here okay now let's say let me make a copy of this say for example I have you know maybe I'll call that main one and just for differentiation we call this as training one now if I execute this program do you think we'll this message be printed or this message will be printed or both of them will be printed what do you think will happen you know from this slide if you understand main method is the entry point your application so where is the main method this one is the main method this is not the main method because it says main one so it treats that method as something else so this is the entry point so you program execution starts here and here which means it only will print this line it the slide okay so if you're a class you know you have this classical program if this class has got hundreds of methods inside that now which way should your control application starts it started in execution that's what is determined by your main method so your main method is basically the entry point in your application okay now so let's go ahead and run this the moment I run that look at this it says welcome to see shop training which is this one but it doesn't print the line but what if I want even that line to be printed and the way to do it is just before you know we know that the program will be terminated here just before that call your main function as well so what happens when we run the program the execution starts here it prints this line and then says since this method call this function call all right that way I need to call this function as well so it goes here it executes this function finishes the call come back into this line and so now if I go ahead and run this program it's going to print both of these messages so it's as simple as that but one thing to keep in mind is that every control application should have a main method which basically tells that's the entry point that's where the program should start executing okay so to sum up this is simple seizure program you know we have a namespace declaration and we need to have class and inside that class we have our main method which signifies the entry point into your application and we are printing using system declaration the namespace declaration indicates that you use system namespace and hence this is basically used organizing cool and it's a collection of classes interfaces structures in and some delegates and main method is the head your application so that's it in this presentation for additional resources please check our website at present a comm and for any interview question say at an c-sharp please these two resources thank you for listening have a great day