Ok, I know what Dynamic and Static means in real life. BUT when used in WebMatrix, what does the "Dynamic" mean?
Look at these two code blocks and notice the Dynamic. I am aware that they are both returning data, but the question, is When should Dynamic be used and when should Static be used. I am confused and
this sounds like programming 101, but I was asked the question and found myself lost. NOTICE the first block is
Public Static dynamicand the Second block is Public Static Int
//block 1
public static dynamic GetPublishedArticles(dynamic category){
Open();
//more codes
//block 2
public static int AddArticle(string title,string summary,string content,
string categoryId,string authorId,string published,string enableComments,string showComments){
Open();
//more codes
Static means you don't need to create any object of a class to call the GetPublishedArticles() method. Like the static void Main() method.
Dynamic is anything which is done at runtime. GetPublishedArticles() method returns a Dynamic type because the category is also fetched at runtime.
Thanks
With Regards
Abhishek Rajiv Luv
"Helpful then please Mark as Answer"
http://www.codeabstract.com/
http://pluralsight.com/training/users/abhishekluv
hmmmm, I have that book, BUT I don't think you want to read that book for the current WebMatrix.
That book dealt with the Original version of Webmatrix released in 2002 and dealt with Web forms. What I am saying is that the book is NOT relevant to WebMatrix of today.
Yes Mike sir's book is the best and very well written.
Thanks
With Regards
Abhishek Rajiv Luv
"Helpful then please Mark as Answer"
http://www.codeabstract.com/
http://pluralsight.com/training/users/abhishekluv
Static means you don't need to create any object of a class to call the GetPublishedArticles() method. Like the static void Main() method.
Dynamic is anything which is done at runtime. GetPublishedArticles() method returns a Dynamic type because the category is also fetched at runtime.
Good answer, but does not address the mean question. When should Dynamic and Static be used? So I have a bunch of classes written, when do I declare anyone of them as dynamic and the other static and what if any is the criteria?
If you to want run some code without any object creation mark that method or function as Static.
Dynamic means at runtime. By using dynamic keyword the work is put on the csc to figure out what is the return type of the method. Dynamic keyword was introduced with .NET 4.0. If you know what will be the return type then use that specific return type otherwise
you can use Dynamic.
Thanks
With Regards
Abhishek Rajiv Luv
"Helpful then please Mark as Answer"
http://www.codeabstract.com/
http://pluralsight.com/training/users/abhishekluv
Marked as answer by yousaid on Dec 18, 2012 12:03 AM
They are not alternatives. Dynamic is a type, like int or string which is decided at runtime instead of compile time. Static is a
modifier which is used to declare a member (method usually) that belongs to the type (class) itself rather than an instance of the class. That means you do not have to "new up" an instance of the class to use the method. The most obvious examples of
static types are the methods on the utility File class - File.Create, File.Exists, File.AppendAllText etc.
So can I in a nutshell say that "Dynamic" is to be used when you don't know what is to be returned be it data type or anything. The system decides based on whatever the user sends to it. And for Static, you the developer is saying, "this is exactely what I
expect and want returned" whenever this action (whatever the action maybe) is taken?
For example, Static Int , you the developer is saying, I expect an Integer to be returned or sent and for
Dynamic, you the user could send in an Integer or a string and if the system can "figure it out" retun or spew out whatever it best thinks suits your action?
I am trying to explain this thing to somebody who does not know and then I realized, it's more complicated than I thought aka, I don't know myself !
You are confusing between Dynamic and Static. Dynamic is type as Mike sir said just like int , string, etc etc only difference is that the compiler figures it out at runtime and not compile time. Static is just a modifier which is used so that you don't
need to create an object of a class before calling any method of that class.
Example : You have a class called "Employee" with a static method GetEmployee(). So in order to call GetEmployee() you don't need to do this if the method has a static modifier.
Employee emp = new Employee();
emp.GetEmployee() ;
With static modifier what the csc is doing is this ClassName.MethodName() - Employee.GetEmployee() and not ObjectName.MethodName().
Thanks
With Regards
Abhishek Rajiv Luv
"Helpful then please Mark as Answer"
http://www.codeabstract.com/
http://pluralsight.com/training/users/abhishekluv
Marked as answer by yousaid on Dec 18, 2012 12:03 AM
For example, Static Int , you the developer is saying, I expect an Integer to be returned
Not quite. There are two separate things there: static, and int.
Static says "you do not need to create an instance of this class in order to call the method".
Int says, "this method returns an int". Static int says "you do not need to create an instance of this class in order to call this method which will return an int".
Like int, dynamic is a type. You can have a method that returns a dynamic and is static:
public static dynamic MyMethod(){
...
}
The reason that dynamic is used in Web Pages so much is because you can add any property you like to a dynamic type. The database helper will iterate the columns in a Select query, and dynamically create properties at runtime that match the column names and make them available on a dynamic object, which is why you can magically do this:
var myThing - db.QuerySingle("SELECT Stuff, Nonsense, AllKinds FROM Anywhere ");
and then you can treat the column names as properties of myThing:
yousaid
Participant
811 Points
334 Posts
Can anyone PLEASE explain Dynamic in WebMatrix?
Dec 16, 2012 11:40 PM|LINK
Ok, I know what Dynamic and Static means in real life. BUT when used in WebMatrix, what does the "Dynamic" mean?
Look at these two code blocks and notice the Dynamic. I am aware that they are both returning data, but the question, is When should Dynamic be used and when should Static be used. I am confused and this sounds like programming 101, but I was asked the question and found myself lost. NOTICE the first block is Public Static dynamic and the Second block is Public Static Int
//block 1 public static dynamic GetPublishedArticles(dynamic category){ Open(); //more codes //block 2 public static int AddArticle(string title,string summary,string content, string categoryId,string authorId,string published,string enableComments,string showComments){ Open(); //more codescheers,
yousaid
Before you head to StackOverflow, they are confused too !! I guess too many experts. Here is their inconclusive answers
http://stackoverflow.com/questions/3105426/what-does-dynamic-actually-mean
Abhishek Luv
Participant
1736 Points
468 Posts
Re: Can anyone PLEASE explain Dynamic in WebMatrix?
Dec 17, 2012 01:31 AM|LINK
Static means you don't need to create any object of a class to call the GetPublishedArticles() method. Like the static void Main() method.
Dynamic is anything which is done at runtime. GetPublishedArticles() method returns a Dynamic type because the category is also fetched at runtime.
With Regards
Abhishek Rajiv Luv
"Helpful then please Mark as Answer"
http://www.codeabstract.com/
http://pluralsight.com/training/users/abhishekluv
yousaid
Participant
811 Points
334 Posts
Re: Can anyone PLEASE explain Dynamic in WebMatrix?
Dec 17, 2012 01:51 AM|LINK
@RameshRajendran
hmmmm, I have that book, BUT I don't think you want to read that book for the current WebMatrix.
That book dealt with the Original version of Webmatrix released in 2002 and dealt with Web forms. What I am saying is that the book is NOT relevant to WebMatrix of today.
Fyi: Back in 2002, Microsoft had a tool named WebMatrix. They later retired it but for whatever reason, they introduced a new tool but kept the old name . I bet many folks have bought that book, only to be disappointed to find out that it talks about a completly different technology. DO NOT buy that book for WebMatrix of today. I will highly recommend Mike's Book
http://www.amazon.com/gp/product/1118050487?ie=UTF8&tag=aspnettelligent-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=1118050487
or
http://www.amazon.com/gp/product/1430240202?ie=UTF8&tag=aspnettelligent-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=1430240202
cheers
yousaid
Abhishek Luv
Participant
1736 Points
468 Posts
Re: Can anyone PLEASE explain Dynamic in WebMatrix?
Dec 17, 2012 01:54 AM|LINK
Yes Mike sir's book is the best and very well written.
With Regards
Abhishek Rajiv Luv
"Helpful then please Mark as Answer"
http://www.codeabstract.com/
http://pluralsight.com/training/users/abhishekluv
yousaid
Participant
811 Points
334 Posts
Re: Can anyone PLEASE explain Dynamic in WebMatrix?
Dec 17, 2012 01:56 AM|LINK
Good answer, but does not address the mean question. When should Dynamic and Static be used? So I have a bunch of classes written, when do I declare anyone of them as dynamic and the other static and what if any is the criteria?
Abhishek Luv
Participant
1736 Points
468 Posts
Re: Can anyone PLEASE explain Dynamic in WebMatrix?
Dec 17, 2012 02:04 AM|LINK
Dear,
If you to want run some code without any object creation mark that method or function as Static.
Dynamic means at runtime. By using dynamic keyword the work is put on the csc to figure out what is the return type of the method. Dynamic keyword was introduced with .NET 4.0. If you know what will be the return type then use that specific return type otherwise you can use Dynamic.
With Regards
Abhishek Rajiv Luv
"Helpful then please Mark as Answer"
http://www.codeabstract.com/
http://pluralsight.com/training/users/abhishekluv
Mikesdotnett...
All-Star
154867 Points
19862 Posts
Moderator
MVP
Re: Can anyone PLEASE explain Dynamic in WebMatrix?
Dec 17, 2012 04:22 AM|LINK
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
yousaid
Participant
811 Points
334 Posts
Re: Can anyone PLEASE explain Dynamic in WebMatrix?
Dec 17, 2012 01:49 PM|LINK
@Abhishek Luv
@Mikesdotnetting
So can I in a nutshell say that "Dynamic" is to be used when you don't know what is to be returned be it data type or anything. The system decides based on whatever the user sends to it. And for Static, you the developer is saying, "this is exactely what I expect and want returned" whenever this action (whatever the action maybe) is taken?
For example, Static Int , you the developer is saying, I expect an Integer to be returned or sent and for Dynamic, you the user could send in an Integer or a string and if the system can "figure it out" retun or spew out whatever it best thinks suits your action?
I am trying to explain this thing to somebody who does not know and then I realized, it's more complicated than I thought aka, I don't know myself !
cheers,
Yousaid
Abhishek Luv
Participant
1736 Points
468 Posts
Re: Can anyone PLEASE explain Dynamic in WebMatrix?
Dec 17, 2012 02:00 PM|LINK
Dear,
You are confusing between Dynamic and Static. Dynamic is type as Mike sir said just like int , string, etc etc only difference is that the compiler figures it out at runtime and not compile time. Static is just a modifier which is used so that you don't need to create an object of a class before calling any method of that class.
Example : You have a class called "Employee" with a static method GetEmployee(). So in order to call GetEmployee() you don't need to do this if the method has a static modifier.
Employee emp = new Employee();
emp.GetEmployee() ;
With static modifier what the csc is doing is this ClassName.MethodName() - Employee.GetEmployee() and not ObjectName.MethodName().
With Regards
Abhishek Rajiv Luv
"Helpful then please Mark as Answer"
http://www.codeabstract.com/
http://pluralsight.com/training/users/abhishekluv
Mikesdotnett...
All-Star
154867 Points
19862 Posts
Moderator
MVP
Re: Can anyone PLEASE explain Dynamic in WebMatrix?
Dec 17, 2012 02:06 PM|LINK
Not quite. There are two separate things there: static, and int. Static says "you do not need to create an instance of this class in order to call the method". Int says, "this method returns an int". Static int says "you do not need to create an instance of this class in order to call this method which will return an int".
Like int, dynamic is a type. You can have a method that returns a dynamic and is static:
public static dynamic MyMethod(){ ... }The reason that dynamic is used in Web Pages so much is because you can add any property you like to a dynamic type. The database helper will iterate the columns in a Select query, and dynamically create properties at runtime that match the column names and make them available on a dynamic object, which is why you can magically do this:
var myThing - db.QuerySingle("SELECT Stuff, Nonsense, AllKinds FROM Anywhere ");and then you can treat the column names as properties of myThing:
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter